On This Page
ReactiveVar
ReactiveVar
is the core class for creating reactive variables in Semantic UI’s reactivity system. It allows you to create values that can be observed and will trigger reactions when changed.
Constructor
Creates a new reactive variable with the given initial value.
Syntax
Parameters
Name | Type | Description |
---|---|---|
initialValue | any | The initial value of the reactive variable |
options | Object | (Optional) Configuration options |
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 |
Usage
Basic Usage
Custom Equality Function
Disabling Cloning for Custom Classes
To avoid mutating the source object naively, by default values are cloned when set. However some objects cannot be naively cloned like custom classes.
Get
Returns the current value of the reactive variable.
Syntax
Alias
value
can be used as an alias for get()
if preferred
Returns
any
- The current value of the reactive variable.
Usage
Retrieving the Current Value
Set
Sets a new value for the reactive variable.
Syntax
Parameters
Name | Type | Description |
---|---|---|
newValue | any | The new value to set |
Usage
Setting a New Value
Subscribe
Creates a subscription that runs the callback whenever the value changes.
Parameters
Name | Type | Description |
---|---|---|
callback | Function | Function to be called when the value changes |
Returns
Object
- A subscription object with a stop()
method to unsubscribe.
Usage
Peek
Returns the current value without triggering reactions.
Syntax
Returns
any
- The current value of the reactive variable.
Usage
Clear
Sets the value of the reactive variable to undefined
and trigger reactions.