createSignal
Creates a simple reactive state with a getter and setter.
When called with a plain value, creates a signal with SignalOptions (name, equals, ownedWrite, unobserved).
When called with a function, creates a writable memo with SignalOptions & MemoOptions (adds id, lazy).
// Plain signalconst [state, setState] = createSignal<T>(value, options?: SignalOptions<T>);// Writable memo (function overload)const [state, setState] = createSignal<T>(fn, initialValue?, options?: SignalOptions<T> & MemoOptions<T>);Import
import { createSignal } from "solid-js";Type signature
function createSignal<T>(): Signal<T | undefined>;function createSignal<T>( value: Exclude<T, Function>, options?: SignalOptions<T>): Signal<T>;function createSignal<T>( fn: ComputeFunction<T>, options?: SignalOptions<T> & MemoOptions<T>): Signal<T>;Parameters
value
Initial value of the state; if empty, the state's type will automatically extended with undefined
options
Optional object with a name for debugging purposes and equals, a comparator function for the previous and next value to allow fine-grained control over the reactivity
Return value
[state: Accessor<T>, setState: Setter<T>]
Examples
const [count, setCount] = createSignal(0);
count(); // 0setCount(1); // explicit valuesetCount((c) => c + 1); // updater// Writable memo: starts as `fn()`, can be locally overwritten by setter.const [user, setUser] = createSignal(() => fetchUser(userId()));
setUser({ ...user(), name: "Alice" }); // optimistic local editRelated types
isEqual
function isEqual<T>(a: T, b: T): boolean;MemoOptions
Options for read-only memos created with createMemo.
Also used in combination with SignalOptions for writable memos
(createSignal(fn) / createOptimistic(fn)).
interface MemoOptions<T> { id?: string; name?: string; transparent?: boolean; equals?: false | ((prev: T, next: T) => boolean); unobserved?: () => void; lazy?: boolean; sync?: boolean;}id
- Type:
string
Stable identifier for the owner hierarchy
name
- Type:
string
Debug name (dev mode only)
transparent
- Type:
boolean
When true, the owner is invisible to the ID scheme -- inherits parent ID and doesn't consume a childCount slot
equals
- Type:
false | ((prev: T, next: T) => boolean)
Custom equality function, or false to always notify subscribers.
Defaults to reference equality (isEqual). Pass a comparator (e.g.
(a, b) => a.id === b.id) for value-based equality, or false to
notify on every recompute regardless of equality.
unobserved
- Type:
() => void
Callback invoked when the computed loses all subscribers
lazy
- Type:
boolean
When true, defers the initial computation until the value is first read, and opts the memo into autodisposal — once it has no remaining subscribers it is torn down and recomputed from scratch on the next read. Use it for compute-on-demand values that should not retain state across idle periods. Non-lazy owned memos live for their owner's lifetime and never autodispose.
sync
- Type:
boolean
Advanced. When true, asserts the compute function returns synchronous
values only (never PromiseLike / AsyncIterable). Skips the
async-shape probe in recompute for a small fixed-cost win per run.
Intended for compiler emissions (_$memo) and library code that
provably returns sync values. Returning a Promise or async iterable
from a sync: true memo is undefined behavior — the value will be
stored as-is and never awaited.
NoInfer
type NoInfer<T extends any> = [T][T extends any ? 0 : never];SignalOptions
Options for plain signals created with createSignal(value) or createOptimistic(value).
interface SignalOptions<T> { name?: string; equals?: false | ((prev: T, next: T) => boolean); ownedWrite?: boolean; unobserved?: () => void;}name
- Type:
string
Debug name (dev mode only)
equals
- Type:
false | ((prev: T, next: T) => boolean)
Custom equality function, or false to always notify subscribers.
Defaults to reference equality (isEqual). Pass a comparator (e.g.
(a, b) => a.id === b.id) for value-based equality, or false to
notify on every write regardless of equality.
ownedWrite
- Type:
boolean
Suppress dev-mode warnings when writing inside an owned scope
unobserved
- Type:
() => void
Callback invoked when the signal loses all subscribers