You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
primitives and handle side effects that respond to dependencies.
16
16
---
17
17
18
-
```ts
19
-
import { createComputed } from"solid-js"
18
+
Creates a reactive computation that runs immediately before render, mainly used to synchronize or write to other reactive primitives, not for DOM side-effects.
20
19
21
-
function createComputed<T>(fn: (v:T) =>T, value?:T):void
20
+
## Import
22
21
22
+
```typescript
23
+
import { createComputed } from"solid-js";
23
24
```
24
25
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.
26
+
## Type Signature
28
27
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).
28
+
```typescript
29
+
function createComputed<Next, Init=Next>(
30
+
* fn: (v:Init|Next) =>Next,
31
+
* value?:Init,
32
+
* options?: { name?:string }
33
+
* ):void;
34
+
```
35
+
36
+
## Parameters
37
+
38
+
### `fn`
39
+
40
+
**Type:**`(v: Init | Next) => Next`
41
+
42
+
The computation function.
43
+
Receives the previous value (or initial value, if set) and returns a new value.
44
+
Runs reactively when dependencies change.
45
+
46
+
### `value`
47
+
48
+
**Type:**`Init`
49
+
50
+
Initial value for the computation.
51
+
If provided, `fn` will never receive `undefined` as its first argument.
52
+
53
+
### `options`
54
+
55
+
**Type:**`{ name?: string }` (optional)
56
+
57
+
Used in development to identify the computed effect.
58
+
59
+
## Return Value
60
+
61
+
**Type:**`void`
62
+
63
+
Registers a computation in the reactive system. Does _not_ return a value.
64
+
65
+
## Usage
66
+
67
+
```typescript
68
+
import { createComputed } from"solid-js";
69
+
70
+
createComputed((prev) => {
71
+
// Compute the next value based on the previous value
72
+
returnprev+1;
73
+
}, 0);
74
+
```
75
+
76
+
## Advanced Usage
77
+
78
+
### Using an Initial Value
79
+
80
+
```typescript
81
+
import { createComputed } from"solid-js";
82
+
83
+
createComputed((prev) => {
84
+
// Compute the next value based on the previous value
- Writing to other reactive primitives, such as signals or stores, based on dependencies.
101
+
- Performing computations that must happen before render, not after.
102
+
- Setting up synchronization between multiple signals without side effects.
103
+
104
+
## How It Works
105
+
106
+
`createComputed` defines a reactive computation that tracks any signals or memos accessed within its function.
107
+
Whenever any of those dependencies change, the computation is rescheduled to run before the next render.
108
+
It is "pure" in the sense that it is not intended to cause side effects outside of updating other reactive primitives.
109
+
110
+
## Details
111
+
112
+
### Development vs Production
113
+
114
+
In development mode, passing a name in the options object can help with debugging and inspection in developer tools.
115
+
In production, the name has no effect.
116
+
117
+
### Error Handling
33
118
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.
119
+
- Errors thrown from the computation function are caught and reported using Solid's error boundaries or global error handlers.
120
+
- Computations created outside a root (e.g. outside `createRoot` or a component) will never be disposed, and a warning is shown in development.
0 commit comments