useSignal
Creates a reactive cell holding one value: the low-level primitive useStore builds on.
Creates a reactive cell holding one value: the low-level primitive useStore builds on. Reads
inside effects and memos subscribe them; writes notify them. Works at module scope.
Import#
ts
import { useSignal } from "@shadow-js/core";
Signature#
ts
function useSignal<T>(value: T, options?: SignalOptions<T>): [Accessor<T>, Setter<T>]
Parameters#
| Name | Description |
|---|---|
value |
Initial value. |
options |
Equality override and a devtools name. Undefined uses the defaults. |
Returns#
A [read, write] pair. The writer also accepts an updater (prev) => next, which runs
untracked, and returns the value it stored.
Throws#
- ShadowError
WRITE_IN_MEMOfrom the writer, in development builds only, when it is called while auseMemocomputation runs (unless that memo setsallowWrites).
Examples#
ts
import { useSignal } from "@shadow-js/core";
const [count, setCount] = useSignal(0);
setCount((n) => n + 1);
console.log(count()); // 1