createSignalEdit this page
The Signal's value starts out equal to the passed first argument
initialValue (or undefined if there are no arguments).
createSignal function returns a pair of functions as a two-element array: a getter (or accessor) and a setter.
In typical use, you would destructure this array into a named Signal like so:
Calling the getter (e.g.,
ready()) returns the current value of the Signal.
Crucial to automatic dependency tracking, calling the getter within a tracking scope causes the calling function to depend on this Signal, so that function will rerun if the Signal gets updated.
Calling the setter (e.g.,
setReady(nextReady)) sets the Signal's value and updates the Signal (triggering dependents to rerun) if the value actually changed (see details below).
The setter takes either the new value for the signal or a function that maps the last value of the signal to a new value as its only argument.
The updated value is also returned by the setter. As an example:
If you want to store a function in a Signal you must use the function form:
However, functions are not treated specially as the
initialValue argument to
createSignal, so you can pass a
function initial value as is:
false | ((prev: T, next: T) => boolean)
|A function that determines whether the Signal's value has changed. If the function returns true, the Signal's value will not be updated and dependents will not rerun. If the function returns false, the Signal's value will be updated and dependents will rerun.
|A name for the Signal. This is useful for debugging.
|If true, the Signal will not be accessible in the devtools.
equals option can be used to customize the equality check used to determine whether the Signal's value has changed.
By default, the equality check is a strict equality check (
If you want to use a different equality check, you can pass a custom function as the
The custom function will be called with the previous and next values of the Signal as arguments.
If the function returns true, the Signal's value will not be updated and dependents will not rerun.
If the function returns false, the Signal's value will be updated and dependents will rerun.
Here's are some examples of this option in use:
name option can be used to give the Signal a name.
This is useful for debugging. The name will be displayed in the devtools.
internal option can be used to hide the Signal from the devtools.
This is useful for Signals that are used internally by a component and should not be exposed to the user.