
Enter a search term above to see results...
Enter a search term above to see results...
Signal
is the core class for creating signals in Semantic UI’s reactivity system. It allows you to create values that can be observed and will trigger reactions when changed.
Creates a new reactive variable with the given initial value.
new Signal(initialValue, options);
Name | Type | Description |
---|---|---|
initialValue | any | The initial value of the reactive variable |
options | Object | (Optional) Configuration options |
Name | Type | Default | Description |
---|---|---|---|
equalityFunction | Function | isEqual | Custom function to determine if the value has changed |
allowClone | boolean | true | Whether to clone the value when getting/setting |
cloneFunction | Function | clone | Custom function to clone the value |
import { Signal } from '@semantic-ui/reactivity';
const counter = new Signal(0);
import { Signal } from '@semantic-ui/reactivity';
const person = new Signal({ name: 'John', age: 30 }, { equalityFunction: (a, b) => a.name === b.name && a.age === b.age});
To avoid mutating the source object naively, by default values are cloned when set. However some objects cannot be naively cloned like custom classes.
import { Signal } from '@semantic-ui/reactivity';
class CustomClass { constructor(value) { this.value = value; }}
const customInstance = new Signal(new CustomClass(42), { allowClone: false, equalityFunction: (a, b) => a.value === b.value});
// The CustomClass instance won't be cloned when accessedconsole.log(customInstance.get().value); // Output: 42
Get
Returns the current value of the reactive variable.
someValue.get();
value
can be used as an alias for get()
if preferred
import { Signal } from '@semantic-ui/reactivity';
const counter = new Signal(0);console.log(counter.value); // Output: 0counter.value = 1;console.log(counter.value); // Output: 1
any
- The current value of the reactive variable.
import { Signal } from '@semantic-ui/reactivity';const counter = new Signal(0);console.log(counter.get()); // Output: 0
Set
Sets a new value for the reactive variable.
someValue.set(newValue);
Name | Type | Description |
---|---|---|
newValue | any | The new value to set |
import { Signal } from '@semantic-ui/reactivity';
const counter = new Signal(0);counter.set(1);console.log(counter.get()); // Output: 1
Subscribe
Creates a subscription that runs the callback whenever the value changes.
Name | Type | Description |
---|---|---|
callback | Function | Function to be called when the value changes |
Object
- A subscription object with a stop()
method to unsubscribe.
import { Signal } from '@semantic-ui/reactivity';
const counter = new Signal(0);const subscription = counter.subscribe((newValue) => { console.log('Counter changed:', newValue);});
counter.set(1); // Console: "Counter changed: 1"subscription.stop(); // Stops the subscription
Peek
Returns the current value without triggering reactions.
someValue.peek();
any
- The current value of the reactive variable.
import { Signal } from '@semantic-ui/reactivity';
const counter = new Signal(0);console.log(counter.peek()); // Output: 0
Clear
Sets the value of the reactive variable to undefined
and trigger reactions.
someValue.clear();
import { Signal } from '@semantic-ui/reactivity';
const counter = new Signal(0);counter.clear();console.log(counter.get()); // Output: undefined