Skip to content

Commit 43e5a9a

Browse files
committed
[docs] merge getters doc into state
1 parent 86f1c5a commit 43e5a9a

File tree

3 files changed

+48
-50
lines changed

3 files changed

+48
-50
lines changed

docs/en/SUMMARY.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
- [State](state.md)
77
- [Mutations](mutations.md)
88
- [Actions](actions.md)
9-
- [Getters](getters.md)
109
- [Data Flow](data-flow.md)
1110
- [Application Structure](structure.md)
1211
- [Middlewares](middlewares.md)

docs/en/getters.md

Lines changed: 0 additions & 46 deletions
This file was deleted.

docs/en/state.md

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ However, this pattern causes the component to rely on the global store singleton
4848

4949
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.
5050

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:
5252

5353
``` js
5454
// MyComponent.js
@@ -58,7 +58,8 @@ However, this pattern causes the component to rely on the global store singleton
5858
// this is where we retrieve state from the store
5959
vuex: {
6060
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`
6263
count: function (state) {
6364
return state.count
6465
}
@@ -79,7 +80,51 @@ However, this pattern causes the component to rely on the global store singleton
7980
}
8081
```
8182

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+
return state.messages.filter(message => {
92+
return message.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+
export function filteredMessages (state) {
110+
return state.messages.filter(message => {
111+
return message.threadID === state.currentThreadID
112+
})
113+
}
114+
```
115+
116+
``` js
117+
// in a component...
118+
import { filteredMessages } from './getters'
119+
120+
export default {
121+
vuex: {
122+
state: {
123+
filteredMessages
124+
}
125+
}
126+
}
127+
```
83128

84129
### Components Are Not Allowed to Directly Mutate State
85130

0 commit comments

Comments
 (0)