Skip to content

Commit d332c43

Browse files
committed
[docs] state
1 parent 0864211 commit d332c43

File tree

1 file changed

+50
-27
lines changed

1 file changed

+50
-27
lines changed

docs/en/state.md

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,74 @@
22

33
### Single State Tree
44

5-
Vuex uses a **single state tree** - that is, this single object contains all your application level state and serves as the "single source of truth". This makes it straightforward to locate a specific piece of state, and allows us to easily take snapshots of the current app state for debugging purposes.
5+
Vuex uses a **single state tree** - that is, this single object contains all your application level state and serves as the "single source of truth". This also means usually you will have only one store for each application. A single state tree makes it straightforward to locate a specific piece of state, and allows us to easily take snapshots of the current app state for debugging purposes.
66

7-
The single state tree does not conflict with modularity - in later chapters we will discuss how to split your state managing logic into sub modules.
7+
The single state tree does not conflict with modularity - in later chapters we will discuss how to split your state and mutations into sub modules.
88

99
### Getting Vuex State into Vue Components
1010

11-
Similar to `data` objects passed to Vue instances, the `state` object, once passed into a Vuex store, becomes reactive powered by [Vue's reactivity system](http://vuejs.org/guide/reactivity.html). This means binding Vuex state to Vue components is as simple as returning it from within a computed property:
11+
So how do we display state inside the store in our Vue components? Here's how:
1212

13-
``` js
14-
// inside a Vue component module
13+
1. Install Vuex and connect your root component to the store:
1514

16-
// import a vuex store
17-
import store from './store'
15+
``` js
16+
import Vue from 'vue'
17+
import Vuex from 'vuex'
18+
import store from './store'
19+
import MyComponent from './MyComponent'
1820

19-
export default {
20-
computed: {
21-
message () {
22-
return store.state.message
21+
// important, teaches Vue components how to
22+
// handle Vuex-related options
23+
Vue.use(Vuex)
24+
25+
var app = new Vue({
26+
el: '#app',
27+
// provide the store using the "store" option.
28+
// this will inject the store instance to all child components.
29+
store,
30+
components: {
31+
MyComponent
32+
}
33+
})
34+
```
35+
36+
2. Inside child components, retrieve state using the `vuex.state` option:
37+
38+
``` js
39+
// MyComponent.js
40+
export default {
41+
template: '...',
42+
data () { ... },
43+
// this is where we retrieve state from the store
44+
vuex: {
45+
state: {
46+
// will bind `store.state.count` on the component as `this.count`
47+
count: function (state) {
48+
return state.count
49+
}
50+
}
2351
}
2452
}
25-
}
26-
```
27-
28-
There's no need to worry about setting up and tearing down listeners, or "connecting" the component to a store. The only thing to remember is that you should **always reference state via `store.state.xxx` inside your computed properties**. Do not cache the reference to a piece of state outside computed properties.
53+
```
2954

30-
> Flux reference: this can be roughly compared to [`mapStateToProps`](https://github.com/rackt/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options) in Redux and [getters](https://optimizely.github.io/nuclear-js/docs/04-getters.html) in NuclearJS.
55+
Note the special `vuex` option block. This is where we specify what state the component will be using from the store. For each property name, we specify a function which receives the entire state tree as the only argument, and then selects and returns a part of the state, or even computes derived state. The returned result will be set on the component using the property name.
3156

32-
Why don't we just use `data` to bind to the state? Consider the following example:
57+
In a lot of cases, the "getter" function can be very succinct using ES2015 arrow functions:
3358

34-
``` js
35-
export default {
36-
data () {
37-
return {
38-
message: store.state.message
59+
``` js
60+
vuex: {
61+
state: {
62+
count: state => state.count
3963
}
4064
}
41-
}
42-
```
65+
```
4366

44-
Because the `data` function does not track any reactive dependencies, we are only getting a static reference to `store.state.message`. When the state is mutated later, the component has no idea that something has changed. In comparison, computed properties track all reactive dependencies when they are evaluated, so they reactively re-evaluate when the related state is mutated.
67+
> Flux reference: this can be roughly compared to [`mapStateToProps`](https://github.com/rackt/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options) in Redux. However, this leverages Vue's computed properties memoization under the hood, thus is more efficient than `mapStateToProps`, and more similar to [reselect](https://github.com/reactjs/reselect) and [NuclearJS getters](https://optimizely.github.io/nuclear-js/docs/04-getters.html).
4568
4669
### Components Are Not Allowed to Directly Mutate State
4770

48-
Using read-only computed properties has another benefit in that it helps emphasizing the rule that **components should never directly mutate Vuex store state**. Because we want every state mutation to be explicit and trackable, all vuex store state mutations must be conducted inside the store's mutation handlers.
71+
It's important to remember that **components should never directly mutate Vuex store state**. Because we want every state mutation to be explicit and trackable, all vuex store state mutations must be conducted inside the store's mutation handlers.
4972

5073
To help enforce this rule, when in [Strict Mode](strict.md), if a store's state is mutated outside of its mutation handlers, Vuex will throw an error.
5174

52-
With this rule in place, our Vue components now hold a lot less responsibility: they are bound to Vuex store state via read-only computed properties, and the only way for them to affect the state is by calling **actions**, which in turn trigger **mutations**. They can still possess and operate on their local state if necessary, but we no longer put any data-fetching or global-state-mutating logic inside individual components.
75+
With this rule in place, our Vue components now hold a lot less responsibility: they are bound to Vuex store state via read-only getters, and the only way for them to affect the state is by somehow triggering **mutations** (which we will discuss later). They can still possess and operate on their local state if necessary, but we no longer put any data-fetching or global-state-mutating logic inside individual components.

0 commit comments

Comments
 (0)