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
Use createMemo to efficiently compute and cache derived values. Prevent
16
16
expensive recalculations and optimize your Solid.js application's performance.
17
17
---
18
18
19
-
Memos let you efficiently use a derived value in many reactive computations.
20
-
`createMemo` creates a readonly reactive value equal to the return value of the given function and makes sure that function only gets executed when its dependencies change.
19
+
The `createMemo` function creates a read-only signal that derives its value from other reactive values.
20
+
The calculated value is memoized: the calculation runs only when dependencies change, and is reused when the value is read.
21
+
When a dependency changes, the calculation re-executes.
22
+
If the new result is equal to the previous result (according to the [`equals`](#equals) option), the memo suppresses downstream updates.
const value =createMemo(() =>computeExpensiveValue(a(), b()))
42
+
### `fn`
37
43
38
-
//read the value
39
-
value()
40
-
```
44
+
-**Type:**`(v: T) => T`
45
+
-**Required:** Yes
41
46
42
-
In Solid, you often don't need to wrap functions in memos; you can alternatively just define and call a regular function to get similar reactive behavior.
43
-
The main difference is when you call the function in multiple reactive settings.
44
-
In this case, when the function's dependencies update, the function will get called multiple times unless it is wrapped in createMemo.
45
-
For example:
47
+
The function that calculates the memo's value.
46
48
47
-
```tsx
48
-
const user =createMemo(() =>searchForUser(username()))
49
-
// compare with: const user = () => searchForUser(username());
50
-
return (
51
-
<ul>
52
-
<li>Your name is {user()?.name}</li>
53
-
<li>
54
-
Your email is <code>{user()?.email}</code>
55
-
</li>
56
-
</ul>
57
-
)
58
-
```
49
+
It receives the value returned from the previous execution as its argument.
50
+
On the first execution, it receives the `value` parameter (if provided) or `undefined`.
51
+
52
+
This function should be pure (it should not modify other reactive values).
53
+
54
+
### `value`
55
+
56
+
-**Type:**`T`
57
+
-**Required:** No
58
+
59
+
The initial value passed to `fn` on its first execution.
When the username signal updates, searchForUser will get called just once.
61
-
If the returned user actually changed, the user memo updates, and then both list items will update automatically.
66
+
An optional configuration object with the following properties:
62
67
63
-
If we had instead defined user as a plain function `() => searchForUser(username())`, then `searchForUser` would have been called twice, once when updating each list item.
68
+
#### `equals`
64
69
65
-
Another key difference is that a memo can shield dependents from updating when the memo's dependencies change but the resulting memo value doesn't.
66
-
Like [createSignal](/reference/basic-reactivity/create-signal), the derived signal made by `createMemo` updates (and triggers dependents to rerun) only when the value returned by the memo function actually changes from the previous value, according to JavaScript's `===` operator.
67
-
Alternatively, you can pass an options object with `equals` set to false to always update the memo when its dependencies change, or you can pass your own `equals` function for testing equality.
The memo function is called with an argument equal to the value returned from the previous execution of the memo function, or, on the first call, equal to the optional second argument to `createMemo`.
70
-
This is useful for reducing computations, such as:
73
+
A function that runs after each execution of `fn` to determine if the memo value has changed.
74
+
It receives the previous and the new value.
75
+
If it returns `true`, the values are considered equal and the memo does not trigger downstream updates.
76
+
77
+
If set to `false` (instead of a function), the memo triggers updates whenever it re-executes, even if the value is unchanged.
78
+
79
+
Defaults to a function that compares values using strict equality (`===`).
80
+
81
+
#### `name`
82
+
83
+
-**Type:**`string`
84
+
-**Required:** No
85
+
86
+
A debug name for the memo.
87
+
It is used for identification in debugging tools like the [Solid Debugger](https://github.com/thetarnav/solid-devtools).
88
+
89
+
## Return value
90
+
91
+
-**Type:**`() => T`
92
+
93
+
`createMemo` returns a read-only accessor function.
94
+
Calling this function returns the current memoized value.
95
+
96
+
## Examples
97
+
98
+
### Basic usage
99
+
100
+
```tsx
101
+
import { createSignal, createMemo, For } from"solid-js";
The memo function should not change other signals by calling setters (it should be "pure").
78
-
This enables Solid to optimize the execution order of memo updates according to their dependency graph, so that all memos can update at most once in response to a dependency change.
0 commit comments