Skip to content

Commit cede53a

Browse files
Fixed Simple State management section to use reactive (#372)
* fix: fixed state management to use reactive * Update src/guide/state-management.md Co-authored-by: Phan An <me@phanan.net> Co-authored-by: Phan An <me@phanan.net>
1 parent 718324f commit cede53a

File tree

1 file changed

+44
-18
lines changed

1 file changed

+44
-18
lines changed

src/guide/state-management.md

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,51 +10,68 @@ If you're coming from React, you may be wondering how vuex compares to [redux](h
1010

1111
## Simple State Management from Scratch
1212

13-
It is often overlooked that the source of truth in Vue applications is the raw `data` object - a component instance only proxies access to it. Therefore, if you have a piece of state that should be shared by multiple instances, you can share it by identity:
13+
It is often overlooked that the source of truth in Vue applications is the reactive `data` object - a component instance only proxies access to it. Therefore, if you have a piece of state that should be shared by multiple instances, you can use a [reactive](/guide/reactivity-fundamentals.html#declaring-reactive-state) method to make an object reactive:
1414

15-
``` js
16-
const sourceOfTruth = {
17-
message: 'Hello'
18-
}
15+
```js
16+
const sourceOfTruth = Vue.reactive({
17+
message: 'Hello'
18+
})
1919

2020
const appA = Vue.createApp({
21-
data () {
21+
data() {
2222
return sourceOfTruth
2323
}
2424
}).mount('#app-a')
2525

2626
const appB = Vue.createApp({
27-
data () {
27+
data() {
2828
return sourceOfTruth
2929
}
3030
}).mount('#app-b')
3131
```
3232

33-
Now whenever `sourceOfTruth` is mutated, both `appA` and `appB` will update their views automatically. Subcomponents within each of these instances would also have access via `this.$root.$data`. We have a single source of truth now, but debugging would be a nightmare. Any piece of data could be changed by any part of our app at any time, without leaving a trace.
33+
```html
34+
<div id="app-a">App A: {{ message }}</div>
35+
36+
<div id="app-b">App B: {{ message }}</div>
37+
```
38+
39+
Now whenever `sourceOfTruth` is mutated, both `appA` and `appB` will update their views automatically. We have a single source of truth now, but debugging would be a nightmare. Any piece of data could be changed by any part of our app at any time, without leaving a trace.
40+
41+
```js
42+
const appB = Vue.createApp({
43+
data() {
44+
return sourceOfTruth
45+
},
46+
mounted() {
47+
sourceOfTruth.message = 'Goodbye' // both apps will render 'Goodbye' message now
48+
}
49+
}).mount('#app-b')
50+
```
3451

3552
To help solve this problem, we can adopt a **store pattern**:
3653

37-
``` js
54+
```js
3855
const store = {
3956
debug: true,
4057

41-
state: {
58+
state: Vue.reactive({
4259
message: 'Hello!'
43-
},
60+
}),
4461

45-
setMessageAction (newValue) {
62+
setMessageAction(newValue) {
4663
if (this.debug) {
4764
console.log('setMessageAction triggered with', newValue)
4865
}
4966

5067
this.state.message = newValue
5168
},
5269

53-
clearMessageAction () {
70+
clearMessageAction() {
5471
if (this.debug) {
5572
console.log('clearMessageAction triggered')
5673
}
57-
74+
5875
this.state.message = ''
5976
}
6077
}
@@ -64,18 +81,27 @@ Notice all actions that mutate the store's state are put inside the store itself
6481

6582
In addition, each instance/component can still own and manage its own private state:
6683

67-
``` js
84+
```html
85+
<div id="app-a">{{sharedState.message}}</div>
86+
87+
<div id="app-b">{{sharedState.message}}</div>
88+
```
89+
90+
```js
6891
const appA = Vue.createApp({
69-
data () {
92+
data() {
7093
return {
7194
privateState: {},
7295
sharedState: store.state
7396
}
97+
},
98+
mounted() {
99+
store.setMessageAction('Goodbye!')
74100
}
75101
}).mount('#app-a')
76102

77103
const appB = Vue.createApp({
78-
data () {
104+
data() {
79105
return {
80106
privateState: {},
81107
sharedState: store.state
@@ -92,4 +118,4 @@ You should never replace the original state object in your actions - the compone
92118

93119
As we continue developing the convention where components are never allowed to directly mutate state that belongs to a store, but should instead dispatch events that notify the store to perform actions, we eventually arrive at the [Flux](https://facebook.github.io/flux/) architecture. The benefit of this convention is we can record all state mutations happening to the store and implement advanced debugging helpers such as mutation logs, snapshots, and history re-rolls / time travel.
94120

95-
This brings us full circle back to [vuex](https://github.com/vuejs/vuex), so if you've read this far it's probably time to try it out!
121+
This brings us full circle back to [Vuex](https://github.com/vuejs/vuex), so if you've read this far it's probably time to try it out!

0 commit comments

Comments
 (0)