Skip to content

Commit 2f59a23

Browse files
committed
Merge pull request #108 from perkola/patch-1
Update middlewares.md
2 parents 6738f59 + d61dcbb commit 2f59a23

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

docs/en/middlewares.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ Vuex stores accept the `middlewares` option that exposes hooks for each mutation
44

55
``` js
66
const myMiddleware = {
7-
onInit (state) {
7+
onInit (state, store) {
88
// record initial state
99
},
10-
onMutation (mutation, state) {
10+
onMutation (mutation, state, store) {
1111
// called after every mutation.
1212
// The mutation comes in the format of { type, payload }
1313
}
@@ -23,14 +23,14 @@ const store = new Vuex.Store({
2323
})
2424
```
2525

26-
By default, a middleware receives the actual `state` object. Since middlewares are primarily used for debugging purposes or data persistence, they are **not allowed to mutate the state**.
26+
By default, a middleware receives the actual `state` object. A middleware can also receive the `store` itself in order to dispatch mutations. Since middlewares are primarily used for debugging purposes or data persistence, they are **not allowed to mutate the state**.
2727

2828
Sometimes a middleware may want to receive "snapshots" of the state, and also compare the post-mutation state with pre-mutation state. Such middlewares must declare the `snapshot: true` option:
2929

3030
``` js
3131
const myMiddlewareWithSnapshot = {
3232
snapshot: true,
33-
onMutation (mutation, nextState, prevState) {
33+
onMutation (mutation, nextState, prevState, store) {
3434
// nextState and prevState are deep-cloned snapshots
3535
// of the state before and after the mutation.
3636
}

0 commit comments

Comments
 (0)