Skip to content

Commit 306a54a

Browse files
committed
createComputed
1 parent 251aee3 commit 306a54a

File tree

1 file changed

+101
-19
lines changed

1 file changed

+101
-19
lines changed

src/routes/reference/secondary-primitives/create-computed.mdx

Lines changed: 101 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,112 @@ description: >-
1515
primitives and handle side effects that respond to dependencies.
1616
---
1717

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.
2019

21-
function createComputed<T>(fn: (v: T) => T, value?: T): void
20+
## Import
2221

22+
```typescript
23+
import { createComputed } from "solid-js";
2324
```
2425

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
2827

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+
return prev + 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
85+
return prev + 1;
86+
}, 0);
87+
```
88+
89+
### Synchronizing State
90+
91+
```typescript
92+
const [source, setSource] = createSignal("initial");
93+
const [copy, setCopy] = createSignal("");
94+
95+
createComputed(() => setCopy(source()));
96+
```
97+
98+
## Common Patterns
99+
100+
- 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
33118

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.
38121

39-
## Arguments
122+
### Performance Characteristics
40123

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. |
124+
- Computed functions are optimized to run only when their tracked dependencies change.
125+
- Avoid doing expensive work or side effects in createComputed; use for reactive synchronization.
126+
- For memoized derived values, use createMemo. For post-render side effects (e.g., DOM or network), use createEffect.

0 commit comments

Comments
 (0)