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: docs/en/state.md
+48-3Lines changed: 48 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -48,7 +48,7 @@ However, this pattern causes the component to rely on the global store singleton
48
48
49
49
By providing the `store` option to the root instance, the store will be injected into all child components of the root and will be available on them as `this.$store`. However it's quite rare that we will need to actually reference it.
50
50
51
-
2. Inside child components, retrieve state using the `vuex.state` option:
51
+
2. Inside child components, retrieve state with **getter** functions in the `vuex.state` option:
52
52
53
53
```js
54
54
// MyComponent.js
@@ -58,7 +58,8 @@ However, this pattern causes the component to rely on the global store singleton
58
58
// this is where we retrieve state from the store
59
59
vuex: {
60
60
state: {
61
-
// will bind `store.state.count` on the component as `this.count`
61
+
// a state getter function, which will
62
+
// bind `store.state.count` on the component as `this.count`
62
63
count:function (state) {
63
64
returnstate.count
64
65
}
@@ -79,7 +80,51 @@ However, this pattern causes the component to rely on the global store singleton
79
80
}
80
81
```
81
82
82
-
> 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).
83
+
### Getters Can Return Derived State
84
+
85
+
Vuex state getters are computed properties under the hood, this means you can leverage them to reactively (and efficiently) compute derived state. For example, say in the state we have an array of `messages` containing all message, and a `currentThreadID` representing a thread that is currently being viewed by the user. What we want to display to the user is a filtered list of messages that belong to the current thread:
86
+
87
+
```js
88
+
vuex: {
89
+
state: {
90
+
filteredMessages:state=> {
91
+
returnstate.messages.filter(message=> {
92
+
returnmessage.threadID===state.currentThreadID
93
+
})
94
+
}
95
+
}
96
+
}
97
+
```
98
+
99
+
Because Vue.js computed properties are automatically cached (and only validated when a reactive dependency changes), you don't need to worry about this function being called on every mutation.
100
+
101
+
> Flux reference: Vuex getters 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).
102
+
103
+
### Sharing Getters Across Multiple Components
104
+
105
+
As you can see, in most cases getters are just pure functions: they take the whole state in, and returns some value. The `filteredMessages` getter may be useful inside multiple components, and it's totally possible to just share the same function between them:
106
+
107
+
```js
108
+
// getters.js
109
+
exportfunctionfilteredMessages (state) {
110
+
returnstate.messages.filter(message=> {
111
+
returnmessage.threadID===state.currentThreadID
112
+
})
113
+
}
114
+
```
115
+
116
+
```js
117
+
// in a component...
118
+
import { filteredMessages } from'./getters'
119
+
120
+
exportdefault {
121
+
vuex: {
122
+
state: {
123
+
filteredMessages
124
+
}
125
+
}
126
+
}
127
+
```
83
128
84
129
### Components Are Not Allowed to Directly Mutate State
0 commit comments