Skip to content

Class: Reaction ​

Defined in: mobx:

Reactions are a special kind of derivations. Several things distinguishes them from normal reactive computations

  1. They will always run, whether they are used by other computations or not. This means that they are very suitable for triggering side effects like logging, updating the DOM and making network requests.
  2. They are not observable themselves
  3. They will always run after any 'normal' derivations
  4. They are allowed to change the state and thereby triggering themselves again, as long as they make sure the state propagates to a stable state in a reasonable amount of iterations.

The state machine of a Reaction is as follows:

  1. after creating, the reaction should be started by calling runReaction or by scheduling it (see also autorun)
  2. the onInvalidate handler should somehow result in a call to this.track(someFunction)
  3. all observables accessed in someFunction will be observed by this reaction.
  4. as soon as some of the dependencies has changed the Reaction will be rescheduled for another run (after the current mutation or transaction). isScheduled will yield true once a dependency is stale and during this period
  5. onInvalidate will be called, and we are back at step 1.

Implements ​

Accessors ​

diffValue ​

Get Signature ​

ts
get diffValue(): 0 | 1;

Defined in: mobx:

Returns ​

0 | 1

Set Signature ​

ts
set diffValue(newValue: 0 | 1): void;

Defined in: mobx:

Parameters ​
ParameterType
newValue0 | 1
Returns ​

void


isDisposed ​

Get Signature ​

ts
get isDisposed(): boolean;

Defined in: mobx:

Returns ​

boolean

Set Signature ​

ts
set isDisposed(newValue: boolean): void;

Defined in: mobx:

Parameters ​
ParameterType
newValueboolean
Returns ​

void


isRunning ​

Get Signature ​

ts
get isRunning(): boolean;

Defined in: mobx:

Returns ​

boolean

Set Signature ​

ts
set isRunning(newValue: boolean): void;

Defined in: mobx:

Parameters ​
ParameterType
newValueboolean
Returns ​

void


isScheduled ​

Get Signature ​

ts
get isScheduled(): boolean;

Defined in: mobx:

Returns ​

boolean

Set Signature ​

ts
set isScheduled(newValue: boolean): void;

Defined in: mobx:

Parameters ​
ParameterType
newValueboolean
Returns ​

void


isTrackPending ​

Get Signature ​

ts
get isTrackPending(): boolean;

Defined in: mobx:

Returns ​

boolean

Set Signature ​

ts
set isTrackPending(newValue: boolean): void;

Defined in: mobx:

Parameters ​
ParameterType
newValueboolean
Returns ​

void

Constructors ​

Constructor ​

ts
new Reaction(
   name_: string | undefined, 
   onInvalidate_: () => void, 
   errorHandler_?: (error: any, derivation: IDerivation) => void, 
   requiresObservable_?: any): Reaction;

Defined in: mobx:

Parameters ​

ParameterType
name_string | undefined
onInvalidate_() => void
errorHandler_?(error: any, derivation: IDerivation) => void
requiresObservable_?any

Returns ​

Reaction

Methods ​

dispose() ​

ts
dispose(): void;

Defined in: mobx:

Returns ​

void

Implementation of ​

IReactionPublic.dispose


getDisposer_() ​

ts
getDisposer_(abortSignal?: GenericAbortSignal): IReactionDisposer;

Defined in: mobx:

Parameters ​

ParameterType
abortSignal?GenericAbortSignal

Returns ​

IReactionDisposer


onBecomeStale_() ​

ts
onBecomeStale_(): void;

Defined in: mobx:

Returns ​

void

Implementation of ​

ts
IDerivation.onBecomeStale_

reportExceptionInDerivation_() ​

ts
reportExceptionInDerivation_(error: any): void;

Defined in: mobx:

Parameters ​

ParameterType
errorany

Returns ​

void


runReaction_() ​

ts
runReaction_(): void;

Defined in: mobx:

internal, use schedule() if you intend to kick off a reaction

Returns ​

void


schedule_() ​

ts
schedule_(): void;

Defined in: mobx:

Returns ​

void


toString() ​

ts
toString(): string;

Defined in: mobx:

Returns a string representation of an object.

Returns ​

string


trace() ​

ts
trace(enterBreakPoint?: boolean): void;

Defined in: mobx:

Parameters ​

ParameterType
enterBreakPoint?boolean

Returns ​

void

Implementation of ​

IReactionPublic.trace


track() ​

ts
track(fn: () => void): void;

Defined in: mobx:

Parameters ​

ParameterType
fn() => void

Returns ​

void

Properties ​

dependenciesState_ ​

ts
dependenciesState_: IDerivationState_;

Defined in: mobx:

Implementation of ​

ts
IDerivation.dependenciesState_

isTracing_ ​

ts
isTracing_: TraceMode;

Defined in: mobx:

Implementation of ​

ts
IDerivation.isTracing_

name_ ​

ts
name_: string;

Defined in: mobx:

Implementation of ​

ts
IDerivation.name_

newObserving_ ​

ts
newObserving_: IObservable[];

Defined in: mobx:

Implementation of ​

ts
IDerivation.newObserving_

observing_ ​

ts
observing_: IObservable[];

Defined in: mobx:

Implementation of ​

ts
IDerivation.observing_

requiresObservable_? ​

ts
optional requiresObservable_?: any;

Defined in: mobx:

warn if the derivation has no dependencies after creation/update

Implementation of ​

ts
IDerivation.requiresObservable_

runId_ ​

ts
runId_: number;

Defined in: mobx:

Id of the current run of a derivation. Each time the derivation is tracked this number is increased by one. This number is globally unique

Implementation of ​

ts
IDerivation.runId_

unboundDepsCount_ ​

ts
unboundDepsCount_: number;

Defined in: mobx:

amount of dependencies used by the derivation in this run, which has not been bound yet.

Implementation of ​

ts
IDerivation.unboundDepsCount_