|
| 1 | +--- |
| 2 | +title: createSignal |
| 3 | +category: Basic Reactivity |
| 4 | +use_cases: >- |
| 5 | + state management, reactive values, component state, form inputs, counters, |
| 6 | + toggles, any reactive data |
| 7 | +tags: |
| 8 | + - signals |
| 9 | + - state |
| 10 | + - reactivity |
| 11 | + - core |
| 12 | + - primitives |
| 13 | +version: "2.0" |
| 14 | +description: >- |
| 15 | + Create reactive state with createSignal, Solid's fundamental primitive. Track |
| 16 | + values that change over time and automatically update your UI when they do. |
| 17 | +--- |
| 18 | + |
| 19 | +V2 TODO! |
| 20 | + |
| 21 | + |
| 22 | +Creates a reactive state primitive consisting of a getter (accessor) and a setter function that forms the foundation of Solid's reactivity system. |
| 23 | +Signals use a pull-based reactivity model where tracking subscriptions (reads) is lightweight, while updates (writes) trigger dependency tracking and effect re-execution, making them optimized for frequent reads and infrequent writes. |
| 24 | + |
| 25 | +## Import |
| 26 | + |
| 27 | +```typescript |
| 28 | +import { createSignal } from "solid-js"; |
| 29 | +``` |
| 30 | + |
| 31 | +## Type signature |
| 32 | + |
| 33 | +```typescript |
| 34 | +function createSignal<T>(): Signal<T | undefined>; |
| 35 | +function createSignal<T>(value: T, options?: SignalOptions<T>): Signal<T>; |
| 36 | + |
| 37 | +type Signal<T> = [get: Accessor<T>, set: Setter<T>]; |
| 38 | + |
| 39 | +type Accessor<T> = () => T; |
| 40 | + |
| 41 | +type Setter<T> = { |
| 42 | + <U extends T>(value: Exclude<U, Function> | ((prev: T) => U)): U; |
| 43 | + <U extends T>(value: (prev: T) => U): U; |
| 44 | + <U extends T>(value: Exclude<U, Function>): U; |
| 45 | + <U extends T>(value: Exclude<U, Function> | ((prev: T) => U)): U; |
| 46 | +}; |
| 47 | + |
| 48 | +interface SignalOptions<T> { |
| 49 | + name?: string; |
| 50 | + equals?: false | ((prev: T, next: T) => boolean); |
| 51 | + internal?: boolean; |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +## Parameters |
| 56 | + |
| 57 | +### `value` |
| 58 | + |
| 59 | +- **Type:** `T` |
| 60 | +- **Default:** `undefined` |
| 61 | + |
| 62 | +The initial value for the signal. |
| 63 | +If no initial value is provided, the signal's type is automatically extended with `undefined`. |
| 64 | + |
| 65 | +### `options` |
| 66 | + |
| 67 | +- **Type:** `SignalOptions<T>` |
| 68 | +- **Default:** `undefined` |
| 69 | + |
| 70 | +Configuration object for the signal. |
| 71 | + |
| 72 | +#### `name` |
| 73 | + |
| 74 | +- **Type:** `string` |
| 75 | +- **Default:** `undefined` |
| 76 | + |
| 77 | +A name for the signal used by debugging tools like [Solid devtools](https://github.com/thetarnav/solid-devtools). |
| 78 | +It works only in development mode and is removed from the production bundle. |
| 79 | + |
| 80 | +#### `equals` |
| 81 | + |
| 82 | +- **Type:** `false | ((prev: T, next: T) => boolean)` |
| 83 | +- **Default:** `false` |
| 84 | + |
| 85 | +A custom comparison function to determine when the signal should update. |
| 86 | +By default, signals use reference equality (`===`) to compare previous and next values. |
| 87 | +When set to `false`, the signal will always update regardless of value equality, which is useful for creating signals that trigger manual updates in the reactive system. |
| 88 | + |
| 89 | +When providing a custom function, it should be pure and return `true` if the values are equal (no update needed) or `false` if they differ (trigger update). |
| 90 | +Impure functions can create unexpected side effects and performance issues. |
| 91 | + |
| 92 | +#### `internal` |
| 93 | + |
| 94 | +- **Type:** `boolean` |
| 95 | +- **Default:** `false` |
| 96 | + |
| 97 | +Marks the signal as internal, preventing it from appearing in development tools. |
| 98 | +This is primarily used by Solid's internal systems. |
| 99 | + |
| 100 | +## Return value |
| 101 | + |
| 102 | +- **Type:** `Signal<T>` |
| 103 | + |
| 104 | +Returns a tuple `[getter, setter]` where: |
| 105 | + |
| 106 | +- **getter**: An accessor function that returns the current value and tracks dependencies when called within a reactive context |
| 107 | +- **setter**: A function that updates the signal's value and notifies all dependent computations |
| 108 | + |
| 109 | +## Examples |
| 110 | + |
| 111 | +### Basic usage |
| 112 | + |
| 113 | +```tsx |
| 114 | +import { createSignal } from "solid-js"; |
| 115 | + |
| 116 | +function Counter() { |
| 117 | + const [count, setCount] = createSignal(0); |
| 118 | + |
| 119 | + return ( |
| 120 | + <div> |
| 121 | + <button onClick={() => setCount(count() + 1)}>+</button> |
| 122 | + <span>{count()}</span> |
| 123 | + </div> |
| 124 | + ); |
| 125 | +} |
| 126 | +``` |
0 commit comments