
Enter a search term above to see results...
Enter a search term above to see results...
Reaction
is a core class in Semantic UI’s reactivity system that manages reactive computations. When you create a reaction, it will automatically re-run whenever any reactive dependencies (Signals) accessed within it change.
Creates a new reaction and runs it immediately.
Reaction.create(callback)
Name | Type | Description |
---|---|---|
callback | Function | Function to run reactively. Receives the reaction instance as its argument. |
Reaction - The created reaction instance.
import { Signal, Reaction } from '@semantic-ui/reactivity';
const counter = new Signal(0);
const reaction = Reaction.create((reaction) => { console.log('Counter value:', counter.get());
if (counter.get() > 5) { reaction.stop(); }});
counter.set(6); // Reaction stops after this updatecounter.set(7); // No output, reaction was stopped
Executes a function without establishing any reactive dependencies.
Reaction.nonreactive(callback)
Name | Type | Description |
---|---|---|
callback | Function | Function to execute non-reactively |
any - The return value of the callback function.
import { Signal, Reaction } from '@semantic-ui/reactivity';
const user = new Signal({ name: 'Alice', lastLogin: new Date() });
Reaction.create(() => { // This will re-run when name changes const name = user.get().name;
// This won't create a dependency on lastLogin const lastLogin = Reaction.nonreactive(() => user.get().lastLogin);
console.log(`${name}'s last login:`, lastLogin);});
Creates a reactive computation that only triggers its parent reaction when its return value changes.
Reaction.guard(callback)
Name | Type | Description |
---|---|---|
callback | Function | Function whose return value is monitored for changes |
any - The current value returned by the callback.
import { Signal, Reaction } from '@semantic-ui/reactivity';
const count = new Signal(0);
Reaction.create(() => { // This will only trigger when the even/odd status changes const isEven = Reaction.guard(() => count.get() % 2 === 0); console.log(`Count is ${isEven ? 'even' : 'odd'}`);});
count.set(2); // No output (still even)count.set(3); // Outputs: "Count is odd"
Executes the reaction’s computation.
reaction.run()
const reaction = Reaction.create(() => { // computation});
reaction.run(); // Manually trigger the reaction
Stops the reaction from running again and cleans up its dependencies.
reaction.stop()
const counter = new Signal(0);const reaction = Reaction.create(() => { if (counter.get() > 3) { reaction.stop(); console.log('Reaction stopped'); }});
Marks the reaction as invalid, scheduling it to run on the next flush cycle.
reaction.invalidate(context?)
Name | Type | Description |
---|---|---|
context | any | (Optional) Additional context for the invalidation |
const reaction = Reaction.create(() => { // computation});
reaction.invalidate(); // Schedule reaction to run again
Boolean indicating if this is the reaction’s first execution.
import { Signal, Reaction } from '@semantic-ui/reactivity';
const data = new Signal({ value: 0 });
Reaction.create((reaction) => { if (reaction.firstRun) { console.log('Initial setup'); } else { console.log('Value updated:', data.get().value); }});
Boolean indicating if the reaction is active (not stopped).
const reaction = Reaction.create((r) => { console.log('Reaction active:', r.active);});
Contains information about what triggered the current reaction run.
{ value: any; trace?: string;} | null
Name | Type | Description |
---|---|---|
value | any | The value that triggered the reaction |
trace | string? | Optional stack trace showing where the change originated |
const counter = new Signal(0);Reaction.create((r) => { console.log('Counter:', counter.get()); if (r.context) { console.log('Changed value:', r.context.value); }});
A Set containing all the dependencies being tracked by this reaction.
Set<Dependency>
const counter = new Signal(0);Reaction.create((r) => { counter.get(); console.log('Number of dependencies:', r.dependencies.size);});
Reaction
proxies several useful methods from Scheduler
so you can call them directly without having to import the Scheduler
class.
Immediately executes all pending reactions.
Reaction.flush()
import { Signal, Reaction } from '@semantic-ui/reactivity';
const number = new Signal(0);Reaction.create(() => { console.log(number.get());});
// Without flush, only final value would be logged[1, 2, 3].forEach(value => { number.set(value); Reaction.flush(); // Forces immediate update});// Logs: 1, 2, 3
Schedules a flush of all pending reactions for the next microtask.
Reaction.scheduleFlush()
import { Signal, Reaction } from '@semantic-ui/reactivity';
const value = new Signal(0);Reaction.create(() => { console.log(value.get());});
value.set(1);Reaction.scheduleFlush(); // Schedule update for next microtask
Registers a callback to execute after the next flush completes.
Reaction.afterFlush(callback)
Name | Type | Description |
---|---|---|
callback | Function | Function to execute after flush completes |
import { Signal, Reaction } from '@semantic-ui/reactivity';
const data = new Signal({ processed: false });
Reaction.create(() => { // Process data const current = data.get();});
data.set({ processed: true });Reaction.afterFlush(() => { console.log('All reactions completed');});
Returns the stack trace of the currently running reaction.
Reaction.getSource()
string | undefined - The stack trace of the current reaction, or undefined if no reaction is running.
Reaction.create(() => { console.log(Reaction.getSource());});