Skip to content

Commit

Permalink
state management
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 21, 2021
1 parent 9a87ffc commit 6f06a44
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 85 deletions.
16 changes: 10 additions & 6 deletions src/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ const nav = [
text: 'Contribution Guide',
link: '/about/contribution-guide'
},
{ text: 'Code of Conduct', link: '/about/coc' }
{ text: 'Code of Conduct', link: '/about/coc' },
{
text: 'The Documentary',
link: 'https://www.youtube.com/watch?v=OrxmtDw4pVI'
}
]
}
]
Expand Down Expand Up @@ -198,7 +202,11 @@ export const sidebar = {
link: '/guide/scaling-up/state-management'
},
{ text: 'Testing', link: '/guide/scaling-up/testing' },
{ text: 'TypeScript', link: '/guide/scaling-up/typescript' }
{ text: 'TypeScript', link: '/guide/scaling-up/typescript' },
{
text: 'Server-Side Rendering (SSR)',
link: '/guide/scaling-up/ssr'
}
]
},
{
Expand Down Expand Up @@ -245,10 +253,6 @@ export const sidebar = {
text: 'Render Functions & JSX',
link: '/guide/extras/render-function'
},
{
text: 'Server-Side Rendering (SSR)',
link: '/guide/extras/ssr'
},
{
text: 'Vue and Web Components',
link: '/guide/extras/web-components'
Expand Down
11 changes: 10 additions & 1 deletion src/guide/extras/reactivity-in-depth.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@ const obj = reactive({
console.log(obj.count === 0) // true
```


### Retaining Reactivity

When we want to use a few properties of the large reactive object, it could be tempting to use destructuring to get properties we want. However, the destructured property would lose the reactivity connection to the original object:
Expand Down Expand Up @@ -444,3 +443,13 @@ watchEffect(async (onInvalidate) => {
An async function implicitly returns a Promise, but the cleanup function needs to be registered immediately before the Promise resolves. In addition, Vue relies on the returned Promise to automatically handle potential errors in the Promise chain.

</div>

## Integration with External State Systems

### State Machines

// TODO `useMachine()` example

### RxJS

The [VueUse](https://vueuse.org/) library provides the [`@vueuse/rxjs`](https://vueuse.org/rxjs/readme.html) add-on for connecting RxJS streams with Vue's reactivity system.
Binary file added src/guide/scaling-up/images/state-flow.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions src/guide/extras/ssr.md → src/guide/scaling-up/ssr.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Server-Side Rendering <Badge text="WIP" />

## Cross-Request State Pollution

In the State Management chapter, we introduced a [simple state management pattern using Reactivity APIs](state-management.html#simple-state-management-with-reactivity-api). In an SSR context, this pattern requires some additional adjustments.

The pattern declares shared state as **singletons**. This means there is only once instance of the reactive object throughout the entire lifecycle of our application. This works as expected in a pure client-side Vue application, since the our application code is initialized fresh for each browser page visit.

However, in an SSR context, the application code is typically initialized only once on the server, when the server boots up. In such case, singletons in our application will be shared across multiple requests handled by the server! If we mutate the shared singleton store with data specific to one user, it can be accidentally leaked to a request from another user. We call this **cross-request state pollution.**

// TODO finish

## Higher Level Solutions

### Nuxt.js
Expand Down
249 changes: 171 additions & 78 deletions src/guide/scaling-up/state-management.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,130 +2,223 @@

## What is State Management?

## Simple State Management from Scratch
Technically, every Vue component instance already "manages" its own reactive state. Take a simple counter component as an example:

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 the [reactive](/api/reactivity-core.html#reactive) method to make an object reactive:
<div class="composition-api">

```js
const { createApp, reactive } = Vue

const sourceOfTruth = reactive({
message: 'Hello'
})

const appA = createApp({
data() {
return sourceOfTruth
}
}).mount('#app-a')
```vue
<script setup>
import { ref } from 'vue'
const appB = createApp({
data() {
return sourceOfTruth
}
}).mount('#app-b')
```
// state
const count = ref(0)
```vue-html
<div id="app-a">App A: {{ message }}</div>
// actions
function increment() {
count.value++
}
</script>
<div id="app-b">App B: {{ message }}</div>
<!-- view -->
<template>{{ count }}</template>
```

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.
</div>
<div class="options-api">

```js
const appB = createApp({
```vue
<script>
export default {
// state
data() {
return sourceOfTruth
return {
count: 0
}
},
mounted() {
sourceOfTruth.message = 'Goodbye' // both apps will render 'Goodbye' message now
// actions
methods: {
increment() {
this.count++
}
}
}).mount('#app-b')
}
</script>
<!-- view -->
<template>{{ count }}</template>
```

To help solve this problem, we can adopt a **store pattern**:
</div>

```js
const store = {
debug: true,
It is a self-contained unit with the following parts:

state: reactive({
message: 'Hello!'
}),
- The **state**, the source of truth that drives our app;
- The **view**, a declarative mapping of the **state**;
- The **actions**, the possible ways the state could change in reaction to user inputs from the **view**.

setMessageAction(newValue) {
if (this.debug) {
console.log('setMessageAction triggered with', newValue)
}
This is a simple representation of the concept of "one-way data flow":

this.state.message = newValue
},
<p style="text-align: center">
<img alt="state flow diagram" src="./images/state-flow.png" width="252px" style="margin: 40px auto">
</p>

clearMessageAction() {
if (this.debug) {
console.log('clearMessageAction triggered')
}
However, the simplicity starts to break down when we have **multiple components that share a common state**:

this.state.message = ''
}
}
1. Multiple views may depend on the same piece of state.
2. Actions from different views may need to mutate the same piece of state.

For case one, a possible workaround is by "lifting" the shared state up to a common ancestor component, and then pass it down as props. However, this quickly gets tedious in component trees with deep hierarchies, leading to another problem known as [Props Drilling](/guide/components/provide-inject.html#props-drilling).

For case two, we often find ourselves resorting to solutions such as reaching for direct parent / child instances via template refs, or trying to mutate and synchronize multiple copies of the state via emitted events. Both of these patterns are brittle and quickly lead to unmaintainable code.

A simpler and more straightforward solution is to extract the shared state out of the components, and manage it in a global singleton. With this, our component tree becomes a big "view", and any component can access the state or trigger actions, no matter where they are in the tree!

## Simple State Management with Reactivity API

<div class="options-api">

In Options API, reactive data is declared using the `data()` option. Internally, the object returned by `data()` is made reactive via the [`reactive()`](/api/reactivity-core.html#reactive) function, which is also available as a public API.

</div>

If you have a piece of state that should be shared by multiple instances, you can use [`reactive()`](/api/reactivity-core.html#reactive) to create a reactive object, and then import it from multiple components:

```js
// store.js
import { reactive } from 'vue'

export const store = reactive({
count: 0
})
```

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.
<div class="composition-api">

In addition, each instance/component can still own and manage its own private state:
```vue
<!-- ComponentA.vue -->
<script setup>
import { store } from './store.js'
</script>
<template>From A: {{ store.count }}</template>
```

```vue-html
<div id="app-a">{{sharedState.message}}</div>
```vue
<!-- ComponentB.vue -->
<script setup>
import { store } from './store.js'
</script>
<div id="app-b">{{sharedState.message}}</div>
<template>From B: {{ store.count }}</template>
```

```js
const appA = createApp({
</div>
<div class="options-api">

```vue
<!-- ComponentA.vue -->
<script>
import { store } from './store.js'
export default {
data() {
return {
privateState: {},
sharedState: store.state
store
}
},
mounted() {
store.setMessageAction('Goodbye!')
}
}).mount('#app-a')
}
</script>
<template>From A: {{ store.count }}</template>
```

```vue
<!-- ComponentB.vue -->
<script>
import { store } from './store.js'
const appB = createApp({
export default {
data() {
return {
privateState: {},
sharedState: store.state
store
}
}
}).mount('#app-b')
}
</script>
<template>From B: {{ store.count }}</template>
```

</div>

Now whenever the `store` object is mutated, both `<ComponentA>` and `<ComponentB>` will update their views automatically - we have a single source of truth now.

However, this also means any component importing `store` can mutate it however they want:

```vue-html{2}
<template>
<button @click="store.count++">
From B: {{ store.count }}
</button>
</template>
```

While this works in simple cases, global state that can be arbitrarily mutated by any component is not going to be very maintainable for the long run. To ensure the state-mutating logic is centralized like the state itself, it is recommended to define methods on the store with names that express the intention of the actions:

```js{6-8}
// store.js
import { reactive } from 'vue'
export const store = reactive({
count: 0,
increment() {
this.count++
}
})
```

```vue-html{2}
<template>
<button @click="store.increment()">
From B: {{ store.count }}
</button>
</template>
```

![State Management](/images/state.png)
<div class="composition-api">

[Try it in the Playground](https://sfc.vuejs.org/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vcmUgPSByZWFjdGl2ZSh7XG4gIGNvdW50OiAwLFxuICBpbmNyZW1lbnQoKSB7XG4gICAgdGhpcy5jb3VudCsrXG4gIH1cbn0pIn0=)

::: tip
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.
</div>
<div class="options-api">

[Try it in the Playground](https://sfc.vuejs.org/#eyJBcHAudnVlIjoiPHNjcmlwdD5cbmltcG9ydCBDb21wb25lbnRBIGZyb20gJy4vQ29tcG9uZW50QS52dWUnXG5pbXBvcnQgQ29tcG9uZW50QiBmcm9tICcuL0NvbXBvbmVudEIudnVlJ1xuICBcbmV4cG9ydCBkZWZhdWx0IHtcbiAgY29tcG9uZW50czoge1xuICAgIENvbXBvbmVudEEsXG4gICAgQ29tcG9uZW50QlxuICB9XG59XG48L3NjcmlwdD5cblxuPHRlbXBsYXRlPlxuICA8Q29tcG9uZW50QSAvPlxuICA8Q29tcG9uZW50QiAvPlxuPC90ZW1wbGF0ZT4iLCJpbXBvcnQtbWFwLmpzb24iOiJ7XG4gIFwiaW1wb3J0c1wiOiB7XG4gICAgXCJ2dWVcIjogXCJodHRwczovL3NmYy52dWVqcy5vcmcvdnVlLnJ1bnRpbWUuZXNtLWJyb3dzZXIuanNcIlxuICB9XG59IiwiQ29tcG9uZW50QS52dWUiOiI8c2NyaXB0PlxuaW1wb3J0IHsgc3RvcmUgfSBmcm9tICcuL3N0b3JlLmpzJ1xuXG5leHBvcnQgZGVmYXVsdCB7XG4gIGRhdGEoKSB7XG4gICAgcmV0dXJuIHtcbiAgICAgIHN0b3JlXG4gICAgfVxuICB9XG59XG48L3NjcmlwdD5cblxuPHRlbXBsYXRlPlxuICA8ZGl2PlxuICAgIDxidXR0b24gQGNsaWNrPVwic3RvcmUuaW5jcmVtZW50KClcIj5cbiAgICAgIEZyb20gQToge3sgc3RvcmUuY291bnQgfX1cbiAgICA8L2J1dHRvbj5cbiAgPC9kaXY+XG48L3RlbXBsYXRlPiIsIkNvbXBvbmVudEIudnVlIjoiPHNjcmlwdD5cbmltcG9ydCB7IHN0b3JlIH0gZnJvbSAnLi9zdG9yZS5qcydcblxuZXhwb3J0IGRlZmF1bHQge1xuICBkYXRhKCkge1xuICAgIHJldHVybiB7XG4gICAgICBzdG9yZVxuICAgIH1cbiAgfVxufVxuPC9zY3JpcHQ+XG5cbjx0ZW1wbGF0ZT5cbiAgPGRpdj5cbiAgICA8YnV0dG9uIEBjbGljaz1cInN0b3JlLmluY3JlbWVudCgpXCI+XG4gICAgICBGcm9tIEI6IHt7IHN0b3JlLmNvdW50IH19XG4gICAgPC9idXR0b24+XG4gIDwvZGl2PlxuPC90ZW1wbGF0ZT4iLCJzdG9yZS5qcyI6ImltcG9ydCB7IHJlYWN0aXZlIH0gZnJvbSAndnVlJ1xuXG5leHBvcnQgY29uc3Qgc3RvcmUgPSByZWFjdGl2ZSh7XG4gIGNvdW50OiAwLFxuICBpbmNyZW1lbnQoKSB7XG4gICAgdGhpcy5jb3VudCsrXG4gIH1cbn0pIn0=)

</div>

:::tip
Note the click handler uses `store.increment()` with the parenthesis - this is necessary to call the method with the proper `this` context since it's not a component method.
:::

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.
Although here we are using a single reactive object as a store, you can also share reactive state created using other [Reactivity APIs](/api/reactivity-core.html) such as `ref()` or `computed()`. The fact that Vue's reactivity system is decoupled from the component model makes it extremely flexible.

## SSR Considerations

This brings us full circle back to [Vuex](https://next.vuex.vuejs.org/), so if you've read this far it's probably time to try it out!
If you are building an application that leverages [Server-Side Rendering (SSR)](./ssr), the above pattern can lead to issues due to the store being a singleton shared across multiple requests. This is discussed in [more details](./ssr#cross-request-state-pollution) in the SSR guide.

## Pinia

## Vuex
While our hand-rolled state management solution will suffice in simple scenarios, there are many more things to consider in large-scale production applications:

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://next.vuex.vuejs.org/), 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/legacy/media/demo.gif).
- Stronger conventions for team collaboration
- Integrating with the Vue DevTools, including timeline, in-component inspection, and time-travel debugging.
- Hot Module Replacement
- Server-Side Rendering support

## Integration with External State Systems
[Pinia](https://pinia.vuejs.org) is a state management library that implements all of the above. It is maintained by the Vue core team, and works with both Vue 2 and Vue 3.

### State Machines
Existing users may be familiar with [Vuex](https://vuex.vuejs.org/), the previous official state management library for Vue. With Pinia serving the same role in the ecosystem, Vuex is now in maintenance mode. It still works, but will no longer receive new features. It is recommended to use Pinia for new applications.

### RxJS
Pinia in fact started as an exploration for how the next iteration of Vuex would look like, incorporating many ideas from core team discussions for Vuex 5. Eventually, we realized that Pinia already implements most of what we wanted in Vuex 5, and decided to make it the new recommendation instead.

### Redux
Compared to Vuex, Pinia provides a simpler API with less ceremony, offers Composition-API-style APIs, and most importantly, has solid type inference support when used with TypeScript.

0 comments on commit 6f06a44

Please sign in to comment.