On This Page
Reaction
Reaction
a core class in Semantic UI’s reactivity system that manages reactive computations. It allows you to create and control reactive contexts which re-run when reactive data sources are updated.
Please see the dedicated section on reactivity to learn more about writing and using reactivity in practice.
Reaction Class
create
Creates a new reaction and runs it immediately. The reaction will re-run whenever any reactive dependencies accessed within the callback change.
Syntax
Parameters
Name | Type | Description |
---|---|---|
callback | Function | The function to run reactively. Receives the reaction instance as its argument. |
Returns
Reaction - The created reaction instance.
Usage
Basic Usage
Stopping a Reaction
You can stop a reaction from within its callback or externally using the returned reaction instance.
Checking for First Run
The firstRun
property allows you to perform specific actions only on the initial execution of the reaction.
Note: Be cautious when using early returns with
firstRun
. If you return early on the first run, the reaction won’t establish dependencies on reactive variables accessed after the return statement.
nonreactive
Runs a function without establishing any reactive dependencies.
Syntax
Parameters
Name | Type | Description |
---|---|---|
func | Function | The function to run non-reactively |
Returns
any - The return value of the provided function.
Usage
guard
Wraps a function to only re-run when its return value changes.
Syntax
Parameters
Name | Type | Description |
---|---|---|
func | Function | The function to guard |
Callback Parameters
Name | Type | Description |
---|---|---|
reaction | class instance | The current running reaction |
See individual reactions for more info.
Returns
any - The return value of the guarded function.
Usage
getSource
Returns the stack trace of the current reaction for debugging purposes.
Debugging Reactivity -
getSource
is designed to be use inside of a breakpoint to determine what reaction is currently running.
Syntax
Returns
string - The stack trace of the current reaction.
Usage
Individual Reaction
You can control each reaction by returning the result of Reaction.create
with the following methods
run
Runs the reaction manually.
Syntax
Usage
invalidate
Marks the reaction as invalid, scheduling it to run again.
Syntax
Parameters
Name | Type | Description |
---|---|---|
context | any | (Optional) Context information for the invalidation |
Usage
stop
Stops the reaction from running again.