Skip to content

Commit e9a7197

Browse files
committed
note about sub-module root
1 parent def2c8d commit e9a7197

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

docs/en/structure.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,26 @@ export default new Vuex.Store({
109109

110110
Here, `cart` module's initial state will be attached to the root state tree as `store.state.cart`. In addition, **all the mutations defined in a sub-module only receives the sub-state-tree they are associated with**. So mutations defined in the `cart` module will receive `store.state.cart` as their first argument.
111111

112+
The root of the sub-state-tree is irreplaceable inside the module itself. For example this won't work:
113+
114+
``` js
115+
const mutations = {
116+
SOME_MUTATION (state) {
117+
state = { ... }
118+
}
119+
}
120+
```
121+
122+
Instead, always store actual state as a property of the sub-tree root:
123+
124+
``` js
125+
const mutations = {
126+
SOME_MUTATION (state) {
127+
state.value = { ... }
128+
}
129+
}
130+
```
131+
112132
Since all modules simply export objects and functions, they are quite easy to test and maintain, and can be hot-reloaded. You are also free to alter the patterns used here to find a structure that fits your preference.
113133

114134
Note that we do not put actions into modules, because a single action may dispatch mutations that affect multiple modules. It's also a good idea to decouple actions from the state shape and the implementation details of mutations for better separation of concerns. If the actions file gets too large, we can turn it into a folder and split out the implementations of long async actions into individual files.

0 commit comments

Comments
 (0)