Skip to content

Commit 77c454a

Browse files
christianalfonigitbook-bot
authored andcommitted
GitBook: [v26] one page modified
1 parent 7911911 commit 77c454a

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

core/defining-state.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,25 @@ The first argument of the function is the state the derived function is attached
9595
Even though derived state is defined as functions you consume them as plain values. You do not have to call the derived function to get the value. Derived functions can also be dynamically added.
9696
{% endhint %}
9797

98+
You can also return a function from the derived. For example:
99+
100+
{% tabs %}
101+
{% tab title="overmind/state.js" %}
102+
```typescript
103+
import { derived } from 'overmind'
104+
105+
export const state = {
106+
users: {},
107+
getUserById: derived(state => id => state.users[id])
108+
}
109+
```
110+
{% endtab %}
111+
{% endtabs %}
112+
113+
The returned value here is indeed a function you call. The cool thing is that the function itself will never change, but whatever state you access when calling the function will be tracked by the caller of the function. So for example if a component uses **getUserById** during rendering it will track what is accessed in the function and continue tracking whatever you access on the returned value.
114+
98115
{% hint style="info" %}
99-
You may use a derived for all sorts of calculations. But sometimes it's better to just use a plain action to create some state than using a derived. Why? Imagine a table component having a lot of rows and columns. We assume the table component also takes care of sorting and filtering and is capable of adding new rows. Now if you solve the sorting and filtering using a derived the following could happen: User adds a new row but it is not displayed in the list because the derived immediately kicked in and filtered it out. Thats not a good user experience. Also in this case the filtering and sorting is clearly started by a simple user interaction \(setting a filter value, clicking on a column,...\) so why not just start an action which creates the new list of sorted and filtered keys? Also the heavy calculation is now very predictable and doesn't cause performance issues because the derived kickes in too often \(Because it could have many dependencies you might didn't think of\)
116+
You may use a derived for all sorts of calculations. But sometimes it's better to just use a plain action to manipulate some state than using a derived. Why? Imagine a table component having a lot of rows and columns. We assume the table component also takes care of sorting and filtering and is capable of adding new rows. Now if you solve the sorting and filtering using a derived the following could happen: User adds a new row but it is not displayed in the list because the derived immediately kicked in and filtered it out. Thats not a good user experience. Also in this case the filtering and sorting is clearly started by a simple user interaction \(setting a filter value, clicking on a column,...\) so why not just start an action which creates the new list of sorted and filtered keys? Also the heavy calculation is now very predictable and doesn't cause performance issues because the derived kickes in too often \(Because it could have many dependencies you might didn't think of\)
100117
{% endhint %}
101118

102119
### Class instances

0 commit comments

Comments
 (0)