Skip to content

Commit 63ad32c

Browse files
committed
derived state section
1 parent 711e76c commit 63ad32c

File tree

1 file changed

+60
-6
lines changed

1 file changed

+60
-6
lines changed

src/routes/reactivity/derived-state.mdx

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,63 @@ title: "Derived State"
33
order: 3
44
---
55

6-
[TODO:
7-
Concept page on Memos
8-
- Improve on existing page
9-
- Move relevant sections to reference and vice versa
10-
- Link out to how-to for derived signals where appropriate
11-
]
6+
Derived state often refers to values that are computed based on other reactive values.
7+
Rather than storing multiple pieces of state and manually keeping them consistent, Solid encourages you to derive new values from existing signals, memos, or stores.
8+
9+
The benefits of derived state:
10+
- You store only the minimal amount of state necessary.
11+
- Relationships between values are described declaratively, making it easier to understand how data flows through your application.
12+
- Derived values are automatically kept in sync with their dependencies.
13+
14+
## Derived signals
15+
16+
The simplest way to create derived state is by using a function that accesses one or more signals.
17+
18+
```tsx
19+
const [count, setCount] = createSignal(0);
20+
21+
// A derived signal: always reflects count * 2
22+
const double = () => count() * 2;
23+
24+
console.log(double()); // 0
25+
setCount(5);
26+
console.log(double()); // 10
27+
```
28+
29+
In the above example, `double` is a derived signal that computes its value based on the `count` signal.
30+
Whenever `count` changes, calling `double()` produces the correct and current value.
31+
Any component or effect that uses `double()`, too, will update automatically when `count` changes.
32+
33+
### Limitations of derived signals
34+
35+
While derived signals are simple and effective for many use cases, they have some limitations:
36+
37+
- **They do not cache their results.**
38+
For derived signal call, the function is re-executed every time it is accessed, even if the underlying signals have not changed.
39+
For trivial computations, this is not a problem, but for expensive calculations (eg. processing large datasets), this can lead to performance issues.
40+
- **They do not create their own reactive scope.**
41+
A derived function runs only when you call it.
42+
Reactivity comes from the signals it touches, but there is no intermediate node in the reactivity graph.
43+
- **Dependent computations can cause unnecessary updates.**
44+
If a derived signal is used within another derived signal or effect, any change to its dependencies will trigger the entire chain to re-evaluate, even if the final value does not change.
45+
46+
In other words, derived signals are best suited for lightweight computations, but they may not be ideal for more complex or performance-sensitive scenarios.
47+
48+
## Memoizing derived state
49+
50+
Memos are a specialized type of derived state that address the limitations of simple derived signals.
51+
They cache results and only re-compute when their dependencies change, making them much more efficient for expensive or frequently accessed calculations.
52+
53+
```tsx
54+
// add example
55+
```
56+
57+
58+
### Advantages of memos
59+
60+
Memos offer several advantages over simple derived signals:
61+
62+
- **Efficient execution** – a memo runs only once per dependency change, rather than re-executing every time it’s accessed.
63+
- **Cached results** – results are stored and reused, avoiding unnecessary recomputation of expensive logic.
64+
- **Change detection** – if dependencies change but the computed value is strictly equal (`===`) to the previous result, no downstream updates are triggered.
65+
- **Automatic tracking** – any signal or memo read inside a memo’s function is automatically tracked, so the memo re-evaluates only when those dependencies change.

0 commit comments

Comments
 (0)