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
Copy file name to clipboardExpand all lines: core/defining-state.md
+28-22Lines changed: 28 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -109,46 +109,39 @@ export const state = {
109
109
You can now use this instance as normal and of course create new ones.
110
110
111
111
{% 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
113
113
{% endhint %}
114
114
115
115
### Serializing class values
116
116
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**:
118
118
119
119
```typescript
120
120
import { SERIALIZE } from'overmind'
121
121
122
122
classUser {
123
-
[SERIALIZE]
124
123
constructor() {
125
124
this.username=''
126
125
this.jwt=''
127
126
}
127
+
toJSON() {
128
+
return {
129
+
[SERIALIZE]: true,
130
+
username: this.username
131
+
}
132
+
}
128
133
}
129
134
```
130
135
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.
137
137
138
138
```typescript
139
139
import { SERIALIZE } from'overmind'
140
140
141
141
classUser {
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 =''
152
145
}
153
146
```
154
147
@@ -185,7 +178,6 @@ Since our user is a class instance we can tell rehydrate what to do, where it is
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.
223
215
224
216
That means the following will behave as expected:
225
217
@@ -229,7 +221,7 @@ That means the following will behave as expected:
229
221
import { User } from'./models'
230
222
231
223
exportconst state = {
232
-
user: null, // Expecting a single value
224
+
user: null, // Expecting an instance or null
233
225
usersList: [], // Expecting an array of values
234
226
usersDictionary: {} // Expecting a dictionary of values
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
+
268
264
## Naming
269
265
270
266
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:
0 commit comments