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
@@ -140,6 +140,8 @@ Here, a side-effect refers to operations or updates that affect state outside of
140
140
In the `Counter` component, a `createEffect` function can be used to update the `doubleCount` state whenever the `count` state changes.
141
141
This keeps the `doubleCount` state in sync with the `count` state, and allows the UI to display the doubled value of `count` to the user.
142
142
143
+
View this example of [`doubleCount` in a `createEffect` in the Solid Playground example](https://playground.solidjs.com/anonymous/b05dddaa-e62a-4c56-b745-5704f3a40194).
144
+
143
145
<TabsCodeBlocks>
144
146
<divid="First render">
145
147
```html title="Counter.tsx" Current count: 0 Doubled count: 0 ```
@@ -153,31 +155,29 @@ This keeps the `doubleCount` state in sync with the `count` state, and allows th
153
155
## Derived state
154
156
155
157
When you want to calculate new state values based on existing state values, you can use derived state.
156
-
Derived state is state that is calculated from other state values.
157
158
This is a useful pattern when you want to display a transformation of a state value to the user, but do not want to modify the original state value or create a new state value.
158
159
159
-
Derived values can be created through using a signal within a function, which can be referred to as a [derived signal](/concepts/derived-values/derived-signals):
160
+
Derived values can be created using a signal within a function, which can be referred to as a [derived signal](/concepts/derived-values/derived-signals).
160
161
161
-
```jsx
162
-
import { createSignal } from"solid-js";
162
+
This approach can be used to simplify the `doubleCount` example above, where the additional signal and effect can be replaced with a derived signal:
While this approach works, it can be inefficient as the `doubleCount` signal will be re-evaluated on every render.
192
-
When a function is computationally expensive, or used multiple times within a component, this can lead to performance issues.
193
-
To avoid this, Solid introduced [Memos](/concepts/derived-values/memos):
191
+
While this approach works for simple use cases, if `doubleCount` is used several times within a component or contains a computationally expensive calculation, it can lead to performance issues.
For cases like this, you can use [Memos](/concepts/derived-values/memos) to store the value of `doubleCount`, which are also referred to as a memoized or cached value.
230
+
When using a memo, the calculation will only run **once** when the value of `count` changes and can be accessed multiple times without re-evaluating for each additional use.
231
+
232
+
Using the [`createMemo`](/reference/basic-reactivity/create-memo) function, you can create a memoized value:
Using the [`createMemo`](/reference/basic-reactivity/create-memo) function, you can create a memoized value that is only re-evaluated when its dependencies change.
227
-
This means that the value of `squaredCount` will only be re-evaluated when the value of `count` changes.
228
-
This is a more efficient approach to calculating derived state, as it only re-evaluates the memoized value when necessary.
269
+
```shellsession title="Console output"
270
+
doubleCountMemo called
271
+
doubleCount called
272
+
doubleCount called
273
+
doubleCount called
274
+
```
275
+
276
+
While accessed multiple times, the `doubleCountMemo` will only re-evaluate and log once.
277
+
This is different from the derived signal, `doubleCount`, which is re-evaluated for each time it is accessed.
278
+
279
+
View a similar [example comparing a derived signal and a memo in the Solid Playground](https://playground.solidjs.com/anonymous/288736aa-d5ba-45f7-a01f-1ac3dcb1b479).
0 commit comments