|
9 | 9 | - primitives |
10 | 10 | - effects |
11 | 11 | - tracking |
12 | | -version: '1.0' |
| 12 | +version: "1.0" |
13 | 13 | description: >- |
14 | 14 | Create immediate reactive computations with createComputed. Build custom |
15 | 15 | primitives and handle side effects that respond to dependencies. |
16 | 16 | --- |
17 | 17 |
|
| 18 | +The `createComputed` function creates a reactive computation that runs _before_ the rendering phase. |
| 19 | +It is primarily used to synchronize state before rendering begins. |
| 20 | + |
| 21 | +## Import |
| 22 | + |
18 | 23 | ```ts |
19 | | -import { createComputed } from "solid-js" |
| 24 | +import { createComputed } from "solid-js"; |
| 25 | +``` |
20 | 26 |
|
21 | | -function createComputed<T>(fn: (v: T) => T, value?: T): void |
| 27 | +## Type |
22 | 28 |
|
| 29 | +```ts |
| 30 | +function createComputed<Next>( |
| 31 | + fn: EffectFunction<undefined | NoInfer<Next>, Next> |
| 32 | +): void; |
| 33 | +function createComputed<Next, Init = Next>( |
| 34 | + fn: EffectFunction<Init | Next, Next>, |
| 35 | + value: Init, |
| 36 | + options?: { name?: string } |
| 37 | +): void; |
| 38 | +function createComputed<Next, Init>( |
| 39 | + fn: EffectFunction<Init | Next, Next>, |
| 40 | + value?: Init, |
| 41 | + options?: { name?: string } |
| 42 | +): void; |
23 | 43 | ``` |
24 | 44 |
|
25 | | -`createComputed` creates a new computation that immediately runs the given function in a tracking scope, thus automatically tracking its dependencies, and automatically reruns the function whenever the dependencies changes. |
26 | | -The function gets called with an argument equal to the value returned from the function's last execution, or on the first call, equal to the optional second argument to `createComputed`. |
27 | | -Note that the return value of the function is not otherwise exposed; in particular, createComputed has no return value. |
| 45 | +## Parameters |
| 46 | + |
| 47 | +### `fn` |
| 48 | + |
| 49 | +- **Type:** `EffectFunction<undefined | NoInfer<Next>, Next> | EffectFunction<Init | Next, Next>` |
| 50 | +- **Required:** Yes |
| 51 | + |
| 52 | +The function that performs the computation. |
| 53 | +It executes immediately to track dependencies and re-runs whenever a dependency changes. |
| 54 | + |
| 55 | +It receives the value returned from the previous execution as its argument. |
| 56 | +On the initial execution, it receives the [`value`](#value) parameter (if provided) or `undefined`. |
| 57 | + |
| 58 | +### `value` |
| 59 | + |
| 60 | +- **Type:** `Init` |
| 61 | +- **Required:** No |
| 62 | + |
| 63 | +The initial value passed to `fn` on its first execution. |
| 64 | + |
| 65 | +### `options` |
| 66 | + |
| 67 | +- **Type:** `{ name?: string }` |
| 68 | +- **Required:** No |
| 69 | + |
| 70 | +An optional configuration object with the following properties: |
28 | 71 |
|
29 | | -`createComputed` is the most immediate form of reactivity in Solid, and is most useful for building other reactive primitives. |
30 | | -For example, some other Solid primitives are built from `createComputed`. |
31 | | -However, it should be used with care, as `createComputed` can easily cause more unnecessary updates than other reactive primitives. |
32 | | -Before using it, consider the closely related primitives [`createMemo`](/reference/basic-reactivity/create-memo) and [`createRenderEffect`](/reference/secondary-primitives/create-render-effect). |
| 72 | +#### `name` |
33 | 73 |
|
34 | | -Like `createMemo`, `createComputed` calls its function immediately on updates (unless you're in a [batch](/reference/reactive-utilities/batch), [effect](/reference/basic-reactivity/create-effect), or [transition](/reference/reactive-utilities/use-transition)). |
35 | | -However, while `createMemo` functions should be pure (not set any signals), `createComputed` functions can set signals. |
36 | | -Related, `createMemo` offers a readonly signal for the return value of the function, whereas to do the same with `createComputed` you would need to set a signal within the function. |
37 | | -If it is possible to use pure functions and `createMemo`, this is likely more efficient, as Solid optimizes the execution order of memo updates, whereas updating a signal within `createComputed` will immediately trigger reactive updates some of which may turn out to be unnecessary. |
| 74 | +- **Type:** `string` |
| 75 | +- **Required:** No |
| 76 | + |
| 77 | +A debug name for the computation. |
| 78 | +It is used for identification in debugging tools like the [Solid Debugger](https://github.com/thetarnav/solid-devtools). |
| 79 | + |
| 80 | +## Return value |
| 81 | + |
| 82 | +- **Type:** `void` |
| 83 | + |
| 84 | +`createComputed` does not return a value. |
| 85 | + |
| 86 | +## Examples |
| 87 | + |
| 88 | +### Basic usage |
| 89 | + |
| 90 | +```tsx |
| 91 | +import { createComputed } from "solid-js"; |
| 92 | +import { createStore } from "solid-js/store"; |
| 93 | + |
| 94 | +type User = { |
| 95 | + name?: string; |
| 96 | +}; |
| 97 | + |
| 98 | +type UserEditorProps = { |
| 99 | + user: User; |
| 100 | +}; |
| 101 | + |
| 102 | +function UserEditor(props: UserEditorProps) { |
| 103 | + const [formData, setFormData] = createStore<User>({ |
| 104 | + name: "", |
| 105 | + }); |
| 106 | + |
| 107 | + // Update the store synchronously when props change. |
| 108 | + // This prevents a second render cycle. |
| 109 | + createComputed(() => { |
| 110 | + setFormData("name", props.user.name); |
| 111 | + }); |
| 112 | + |
| 113 | + return ( |
| 114 | + <form> |
| 115 | + <h1>Editing: {formData.name}</h1> |
| 116 | + <input |
| 117 | + value={formData.name} |
| 118 | + onInput={(e) => setFormData("name", e.currentTarget.value)} |
| 119 | + /> |
| 120 | + </form> |
| 121 | + ); |
| 122 | +} |
| 123 | +``` |
38 | 124 |
|
39 | | -## Arguments |
| 125 | +## Related |
40 | 126 |
|
41 | | -| Name | Type | Description | |
42 | | -| :------ | :------------ | :----------------------------------------- | |
43 | | -| `fn` | `(v: T) => T` | The function to run in a tracking scope. | |
44 | | -| `value` | `T` | The initial value to pass to the function. | |
| 127 | +- [`createMemo`](/reference/basic-reactivity/create-memo) |
| 128 | +- [`createRenderEffect`](/reference/secondary-primitives/create-render-effect) |
0 commit comments