State
Solid.js-inspired state management library. While it is intended to be used in conjunction with the UI library, state management has many use-cases outside of this.
Functions
State.createSignal
Creates a "signal" with a given initial value. Signals form the backbone of a reactive system, allowing other code to observe their changes and run side effects accordingly.
When read from within a tracking scope (e.g. createEffect
), will add the signal to the list of observed signals in the scope,
causing the scope's observer to react to updates.
State.createSignal(value: any): fun(): any, fun(value: any)
Parameters
Name | Type | Default Value | Description |
---|---|---|---|
value | any | Required | Initial value for the signal. |
Returns
Name | Type | Description |
---|---|---|
get | fun(): any | Returns the current value of the signal. |
set | fun(value: any) | Sets the value of the signal and notifies all observers of the change. |
State.createEffect
Creates an "effect" which is triggered when any of the signals it uses are updated. Simply calling the getter of a signal within the callback will cause the effect to observe changes.
All observers and any child effects will be cleaned up when the effect is re-run, allowing you to conditionally create effects or listen to a second signal based on the value of the first.
State.createEffect(callback: fun())
Parameters
Name | Type | Default Value | Description |
---|---|---|---|
callback | fun() | Required | Callback to execute when any observed signals are updated. |
State.createMemo
Memoises a value so that it's only recomputed when one of the observed signals changes. The value returned is itself a signal, so that downstream effects are updated when the memoised value is recomputed.
State.createMemo(callback: fun()): fun(): any
Parameters
Name | Type | Default Value | Description |
---|---|---|---|
callback | fun() | Required | Callback to execute when any observed signals are updated. Same as an effect. |
Returns
Name | Type | Description |
---|---|---|
get | fun(): any | Returns the current value of the memo. Same as a signal's getter. |
State.untrack
Executes the callback immediately and without a tracking scope. This allows for reading the values of signals inside of effect callbacks without observing future changes.
State.untrack(callback: fun(): ...any): ...any
Parameters
Name | Type | Default Value | Description |
---|---|---|---|
callback | fun(): ...any | Required | No description |
Returns
Name | Type | Description |
---|---|---|
... | ...any | The values returned by callback . |
State.onCleanup
Registers a cleanup hook within the current tracking scope. Will be called when the scope is disposed (e.g. when the parent of a nested effect is rerun, or a component is unmounted).
Any signals read within the callback will not be tracked.
State.onCleanup(callback: fun())
Parameters
Name | Type | Default Value | Description |
---|---|---|---|
callback | fun() | Required | No description |