Skip to content

Commit 63a979a

Browse files
christianalfonigitbook-bot
authored andcommitted
GitBook: [master] 2 pages modified
1 parent bfb9b3f commit 63a979a

File tree

2 files changed

+14
-59
lines changed

2 files changed

+14
-59
lines changed

core/defining-state.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ export const state = {
113113
{% endtab %}
114114
{% endtabs %}
115115

116+
{% hint style="warning" %}
117+
It is import that you do **NOT** use arrow functions on your methods. The reason is that this binds the context of the method to the instance itself, meaning that Overmind is unable to proxy access and allow you to do tracked mutations
118+
{% endhint %}
119+
116120
You can now use this instance as normal and of course create new ones.
117121

118122
{% hint style="info" %}
@@ -383,12 +387,12 @@ You set an **initial** state and then you create a relationship between the diff
383387
export const login = async ({ state, effects }) => {
384388
return state.mode.authenticating(async () => {
385389
try {
386-
const user = await effects.api.getUser()
387-
state.mode.authenticated(() => {
390+
const user = effects.api.getUser()
391+
return state.mode.authenticated(() => {
388392
state.user = user
389393
})
390394
} catch (error) {
391-
state.mode.unauthenticated(() => {
395+
return state.mode.unauthenticated(() => {
392396
state.error = error
393397
})
394398
}
@@ -399,9 +403,9 @@ export const logout = async ({ state, effects }) => {
399403
return state.mode.unauthenticating(async () => {
400404
try {
401405
await effects.api.logout()
402-
state.mode.unauthenticated()
406+
return state.mode.unauthenticated()
403407
} catch (error) {
404-
state.mode.authenticated(() => {
408+
return state.mode.authenticated(() => {
405409
state.error = error
406410
})
407411
}
@@ -411,8 +415,11 @@ export const logout = async ({ state, effects }) => {
411415
{% endtab %}
412416
{% endtabs %}
413417

414-
{% hint style="info" %}
415-
If your transition runs asynchronously you should return the transition to ensure that the action execution is tracked
418+
{% hint style="warning" %}
419+
There are two important rules for predictable transitions:
420+
421+
1. The transition should always **return**
422+
2. **Async** transitions should not **mutate** state
416423
{% endhint %}
417424

418425
What is important to realize here is that our logic is separated into **allowable** transitions. That means when we are waiting for the user on **line 4** and some other logic has changed the state to **unauthenticated** in the meantime, the user will not be set, as the **authenticated** transition is now not possible. This is what state machines do. They group logic into states that are allowed to run, preventing invalid logic to run.

guides-1/server-side-rendering.md

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -103,55 +103,3 @@ export const onInitialize: OnInitialize = ({ state }) => {
103103
If you are using state first routing, make sure you prevent the router from firing off the initial route, as this is not needed.
104104
{% endhint %}
105105

106-
## OnInitialize
107-
108-
The `onInitialized` action does not run on the server. The reason is that it is considered a side effect you might not want to run, so we do not force it. If you do want to run an action as Overmind fires up both on the client and the server you can rather create a custom action for it.
109-
110-
{% tabs %}
111-
{% tab title="overmind/actions.js" %}
112-
```javascript
113-
export const initialize = () => {
114-
// Whatever...
115-
}
116-
```
117-
{% endtab %}
118-
119-
{% tab title="client/index.js" %}
120-
```typescript
121-
import { createOvermind } from 'overmind'
122-
import { config } from './overmind'
123-
124-
const overmind = createOvermind(config)
125-
overmind.actions.initialize()
126-
```
127-
{% endtab %}
128-
129-
{% tab title="server/index.js" %}
130-
```javascript
131-
import { createOvermindSSR } from 'overmind'
132-
import { config } from '../client/overmind'
133-
134-
export default async (req, res) => {
135-
const overmind = createOvermindSSR(config)
136-
await overmind.actions.initialize()
137-
138-
const html = renderToString(
139-
// Whatever implementation your view layer provides
140-
)
141-
142-
res.send(`
143-
<html>
144-
<body>
145-
<div id="app">${html}</div>
146-
<script>
147-
window.__OVERMIND_MUTATIONS = ${JSON.stringify(overmind.hydrate())}
148-
</script>
149-
<script src="/scripts/app.js"></script>
150-
</body>
151-
</html>
152-
`)
153-
}
154-
```
155-
{% endtab %}
156-
{% endtabs %}
157-

0 commit comments

Comments
 (0)