Skip to content

Commit 6cb000c

Browse files
authored
Migrate: State management (#94)
1 parent bbe2107 commit 6cb000c

File tree

4 files changed

+104
-1
lines changed

4 files changed

+104
-1
lines changed

src/.vuepress/config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ const sidebar = {
5252
{
5353
title: 'Scaling Up',
5454
collapsable: false,
55-
children: ['/guide/routing']
55+
children: [
56+
'/guide/routing',
57+
'/guide/state-management'
58+
]
5659
},
5760
{
5861
title: 'Migration to Vue 3',

src/.vuepress/public/images/state.png

12.6 KB
Loading

src/guide/accessibility.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Accessiblity
2+
13
An accessible website is a website that can be used by anyone — be that a person with a disability, someone on a slow connection, or someone whose hardware is dated or broken. It's easy to make a website assuming that all our users are using a keyboard, mouse, and screen, and have a way of hearing the sound produced by our websites, but that often isn't true: millions of people around the world have disabilities and are unable to use all the functionality of a computer in the same way most developers do.
24

35
While many people with permanent disabilities might have tooling to help them, they're also relying on the people building the websites to make them accessible and work well with the tooling.

src/guide/state-management.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# State Management
2+
3+
## Official Flux-Like Implementation
4+
5+
Large applications can often grow in complexity, due to multiple pieces of state scattered across many components and the interactions between them. To solve this problem, Vue offers [vuex](https://github.com/vuejs/vuex), our own Elm-inspired state management library. It even integrates into [vue-devtools](https://github.com/vuejs/vue-devtools), providing zero-setup access to [time travel debugging](https://raw.githubusercontent.com/vuejs/vue-devtools/master/media/demo.gif).
6+
7+
<!--TODO: use an updated tutorial for 3.0?-->
8+
<div class="vue-mastery"><a href="https://www.vuemastery.com/courses/mastering-vuex/intro-to-vuex/" target="_blank" rel="sponsored noopener" title="Vuex Tutorial">Watch a video explanation on Vue Mastery</a></div>
9+
10+
### Information for React Developers
11+
12+
If you're coming from React, you may be wondering how vuex compares to [redux](https://github.com/reactjs/redux), the most popular Flux implementation in that ecosystem. Redux is actually view-layer agnostic, so it can easily be used with Vue via [simple bindings](https://classic.yarnpkg.com/en/packages?q=redux%20vue&p=1). Vuex is different in that it _knows_ it's in a Vue app. This allows it to better integrate with Vue, offering a more intuitive API and improved development experience.
13+
14+
## Simple State Management from Scratch
15+
16+
It is often overlooked that the source of truth in Vue applications is the raw `data` object - a Vue 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:
17+
18+
``` js
19+
const sourceOfTruth = {
20+
message: 'Hello'
21+
}
22+
23+
const appA = Vue.createApp({
24+
data () {
25+
return sourceOfTruth
26+
}
27+
}).mount('#app-a')
28+
29+
const appB = Vue.createApp({
30+
data () {
31+
return sourceOfTruth
32+
}
33+
}).mount('#app-b')
34+
```
35+
36+
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.
37+
38+
To help solve this problem, we can adopt a **store pattern**:
39+
40+
``` js
41+
const store = {
42+
debug: true,
43+
44+
state: {
45+
message: 'Hello!'
46+
},
47+
48+
setMessageAction (newValue) {
49+
if (this.debug) {
50+
console.log('setMessageAction triggered with', newValue)
51+
}
52+
53+
this.state.message = newValue
54+
},
55+
56+
clearMessageAction () {
57+
if (this.debug) {
58+
console.log('clearMessageAction triggered')
59+
}
60+
61+
this.state.message = ''
62+
}
63+
}
64+
```
65+
66+
Notice all actions that mutate the store's state are put inside the store itself. This type of centralized state management makes it easier to understand what type of mutations could happen and how they are triggered. Now when something goes wrong, we'll also have a log of what happened leading up to the bug.
67+
68+
In addition, each instance/component can still own and manage its own private state:
69+
70+
``` js
71+
const appA = Vue.createApp({
72+
data () {
73+
return {
74+
privateState: {},
75+
sharedState: store.state
76+
}
77+
}
78+
}).mount('#app-a')
79+
80+
const appB = Vue.createApp({
81+
data () {
82+
return {
83+
privateState: {},
84+
sharedState: store.state
85+
}
86+
}
87+
}).mount('#app-b')
88+
```
89+
90+
![State Management](/images/state.png)
91+
92+
::: tip
93+
You should never replace the original state object in your actions - the components and the store need to share reference to the same object in order for mutations to be observed.
94+
:::
95+
96+
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.
97+
98+
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)