Skip to content

Commit e55df06

Browse files
christianalfonigitbook-bot
authored andcommitted
GitBook: [master] one page modified
1 parent 7e4e6cb commit e55df06

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

core/defining-state.md

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -109,46 +109,39 @@ export const state = {
109109
You can now use this instance as normal and of course create new ones.
110110

111111
{% hint style="info" %}
112-
Even though you can use **getters** as normal, they do not cache like **derived**. **Derived** is a concept of the state tree itself. It is unlikely that you need heavy computation within a single class instance though, it is typically across class instances
112+
Even though you can use **getters** as normal, they do not cache like **derived**. **Derived** is a concept of the state tree itself. It is unlikely that you need heavy computation within a single class instance though, it is typically across class instances, where **derived** fits the bill
113113
{% endhint %}
114114

115115
### Serializing class values
116116

117-
If you have an application that needs to serialize the state, for example to local storage or server side rendering, you can still use class instances with Overmind. Overmind exposes a symbol called **SERIALIZE** that you can attach to your class.
117+
If you have an application that needs to serialize the state, for example to local storage or server side rendering, you can still use class instances with Overmind. This works out of the box except when you add the **toJSON** method to a class. In that case you will need to add a symbol called **SERIALIZE**:
118118

119119
```typescript
120120
import { SERIALIZE } from 'overmind'
121121

122122
class User {
123-
[SERIALIZE]
124123
constructor() {
125124
this.username = ''
126125
this.jwt = ''
127126
}
127+
toJSON() {
128+
return {
129+
[SERIALIZE]: true,
130+
username: this.username
131+
}
132+
}
128133
}
129134
```
130135

131-
There are two purposes to using **SERIALIZE**.
132-
133-
1. When using Typescript you will be able to get type safety in your rehydration of state
134-
2. The devtools requires this to properly display values as class instances
135-
136-
You can also safely use **toJSON**, though the for the two reasons above you want to add **SERIALIZE**:
136+
If you use **Typescript** you want to add **SERIALIZE** to the class itself as this will give you type safety when rehydrating the state.
137137

138138
```typescript
139139
import { SERIALIZE } from 'overmind'
140140

141141
class User {
142-
constructor() {
143-
this.username = ''
144-
this.jwt = ''
145-
}
146-
toJSON() {
147-
return {
148-
[SERIALIZE]: true,
149-
username: this.username
150-
}
151-
}
142+
[SERIALIZE] = true
143+
username = ''
144+
jwt = ''
152145
}
153146
```
154147

@@ -185,7 +178,6 @@ Since our user is a class instance we can tell rehydrate what to do, where it is
185178
import { SERIALIZE } from 'overmind'
186179

187180
class User {
188-
[SERIALIZE]
189181
constructor() {
190182
this.username = ''
191183
this.jwt = ''
@@ -219,7 +211,7 @@ export const updateState = ({ state }) => {
219211
{% endtab %}
220212
{% endtabs %}
221213

222-
It does not matter if the state is a value, an array of values or a dictionary of values, rehydrate will understand it.
214+
It does not matter if the state value is a class instance, an array of class instances or a dictionary of class instances, rehydrate will understand it.
223215

224216
That means the following will behave as expected:
225217

@@ -229,7 +221,7 @@ That means the following will behave as expected:
229221
import { User } from './models'
230222

231223
export const state = {
232-
user: null, // Expecting a single value
224+
user: null, // Expecting an instance or null
233225
usersList: [], // Expecting an array of values
234226
usersDictionary: {} // Expecting a dictionary of values
235227
}
@@ -265,10 +257,24 @@ export const updateState = ({ state }) => {
265257
{% endtab %}
266258
{% endtabs %}
267259

260+
{% hint style="info" %}
261+
Note that **rehydrate** gives you full type safety when adding the **SERIALIZE** symbol to your classes. This is a huge benefit as Typescript will yell at you when the state structure changes, related to the rehydration
262+
{% endhint %}
263+
268264
## Naming
269265

270266
Each value needs to sit behind a name. Naming can be difficult, but we have some help. Even though we eventually do want to consume our application through a user interface we ideally want to avoid naming things specifically related to the environment where we show the user interface. Things like **page**, **tabs**, **modal** etc. are specific to a browser experience, maybe related to a certain size. We want to avoid those names as they should not dictate which elements are to be used with the state, that is up to the user interface to decide later. So here are some generic terms to use instead:
271267

268+
269+
270+
{% hint style="info" %}
271+
272+
{% endhint %}
273+
274+
{% hint style="info" %}
275+
276+
{% endhint %}
277+
272278
* page: **mode**
273279
* tabs: **sections**
274280
* modal: **editUser.active**

0 commit comments

Comments
 (0)