Skip to content

Commit 6857feb

Browse files
docs(website): add more information about derived
1 parent 28bac43 commit 6857feb

File tree

4 files changed

+101
-2
lines changed

4 files changed

+101
-2
lines changed

packages/overmind-website/api/derive.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,13 @@ The function defining your derived state receives two arguments. The first argum
1212
```marksy
1313
h(Notice, null, "Accessing **rootState** might cause unnecessary updates to the derived function as it will track more state, though typically not an issue")
1414
```
15+
16+
An other use case for derived is to return a function. This allows you to insert functions into your state tree which can execute logic, even based on existing state. Even the function itself might be changed out based on the state of the application.
17+
18+
```marksy
19+
h(Example, { name: "api/derive_function" })
20+
```
21+
22+
```marksy
23+
h(Notice, null, "Using state inside the returned function will not be tracked. You have to access the state in the scope of the derived function")
24+
```
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
export default (ts) =>
2+
ts
3+
? [
4+
{
5+
fileName: 'app/state.ts',
6+
code: `
7+
import { Derive } from 'overmind'
8+
9+
export type User = {
10+
name: string
11+
}
12+
13+
export type State = {
14+
user: User
15+
tellUser: Derive<State, (message: string) => {}>
16+
}
17+
18+
export const state: State = {
19+
user: {
20+
name: 'John'
21+
},
22+
tellUser: (state) =>
23+
(message) => console.log(message, ', ' + state.user.name)
24+
}
25+
`,
26+
},
27+
]
28+
: [
29+
{
30+
fileName: 'app/state.js',
31+
code: `
32+
export const state = {
33+
user: {
34+
name: 'John'
35+
},
36+
tellUser: (state) =>
37+
(message) => console.log(message, ', ' + state.user.name)
38+
}
39+
`,
40+
},
41+
]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export default (ts) =>
2+
ts
3+
? [
4+
{
5+
fileName: 'app/state.ts',
6+
code: `
7+
import { Derive } from 'overmind'
8+
9+
export type State = {
10+
title: string
11+
upperTitle: Derive<State, string>
12+
}
13+
14+
export const state: State = {
15+
title: 'My awesome title',
16+
upperTitle: state => state.title.toUpperCase()
17+
}
18+
`,
19+
},
20+
]
21+
: [
22+
{
23+
fileName: 'app/state.js',
24+
code: `
25+
export const state = {
26+
title: 'My awesome title',
27+
upperTitle: state => state.title.toUpperCase()
28+
}
29+
`,
30+
},
31+
]

packages/overmind-website/guides/beginner/02_definingstate.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,25 @@ Are things loading or not, is the user logged in or not ? These are typical usag
4848

4949
All values, with the exception of booleans, can also be **null**. Non existing. You can have a non existing object, array, string or number. That means if we have not selected a tab both the string version and number version would have the value **null**.
5050

51+
## Deriving state
52+
53+
Not all state should be explicitly defined. Some state are values based off of other state. This is what we call **derived state**. A simple example of this would be:
54+
55+
```marksy
56+
h(Example, { name: "guide/definingstate/derived" })
57+
```
58+
59+
The first argument of the function is the state the derived function is attached to. A second argument is also passed and that is the root state of the application, allowing you to access whatever you would need. Two important traits of the derived function is:
60+
61+
1. The state accessed is tracked
62+
2. The value returned is cached
63+
64+
That means the function only runs when accessed and the depending state has changed since last access.
65+
66+
```marksy
67+
h(Notice, null, "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")
68+
```
69+
5170
## Defining state
5271

5372
We define the state of the application in **state** files. For example, the top level state could be defined as:
@@ -56,8 +75,6 @@ We define the state of the application in **state** files. For example, the top
5675
h(Example, { name: "guide/definingstate/define" })
5776
```
5877

59-
Note that we are exporting variables from our state module. We use **let** for values that can be replaced and **const** for values that can not be replaced. That would typically be derived values, but you might also have arrays or objects that should not be replaced.
60-
6178
As your application grows you will most likely move parts of the state to their own namespaces. An example of that could be:
6279

6380
```marksy

0 commit comments

Comments
 (0)