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
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>
Copy file name to clipboardExpand all lines: src/guide/state-management.md
+44-18Lines changed: 44 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -10,51 +10,68 @@ If you're coming from React, you may be wondering how vuex compares to [redux](h
10
10
11
11
## Simple State Management from Scratch
12
12
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:
14
14
15
-
```js
16
-
constsourceOfTruth= {
17
-
message:'Hello'
18
-
}
15
+
```js
16
+
constsourceOfTruth=Vue.reactive({
17
+
message:'Hello'
18
+
})
19
19
20
20
constappA=Vue.createApp({
21
-
data() {
21
+
data() {
22
22
return sourceOfTruth
23
23
}
24
24
}).mount('#app-a')
25
25
26
26
constappB=Vue.createApp({
27
-
data() {
27
+
data() {
28
28
return sourceOfTruth
29
29
}
30
30
}).mount('#app-b')
31
31
```
32
32
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
+
<divid="app-a">App A: {{ message }}</div>
35
+
36
+
<divid="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
+
constappB=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
+
```
34
51
35
52
To help solve this problem, we can adopt a **store pattern**:
@@ -64,18 +81,27 @@ Notice all actions that mutate the store's state are put inside the store itself
64
81
65
82
In addition, each instance/component can still own and manage its own private state:
66
83
67
-
```js
84
+
```html
85
+
<divid="app-a">{{sharedState.message}}</div>
86
+
87
+
<divid="app-b">{{sharedState.message}}</div>
88
+
```
89
+
90
+
```js
68
91
constappA=Vue.createApp({
69
-
data() {
92
+
data() {
70
93
return {
71
94
privateState: {},
72
95
sharedState:store.state
73
96
}
97
+
},
98
+
mounted() {
99
+
store.setMessageAction('Goodbye!')
74
100
}
75
101
}).mount('#app-a')
76
102
77
103
constappB=Vue.createApp({
78
-
data() {
104
+
data() {
79
105
return {
80
106
privateState: {},
81
107
sharedState:store.state
@@ -92,4 +118,4 @@ You should never replace the original state object in your actions - the compone
92
118
93
119
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.
94
120
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