State Management

Official Flux-Like Implementation

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 vuexState Management - 图1, our own Elm-inspired state management library. It even integrates into vue-devtoolsState Management - 图2, providing zero-setup access to time travel debuggingState Management - 图3.

Information for React Developers

If you’re coming from React, you may be wondering how vuex compares to reduxState Management - 图4, the most popular Flux implementation in that ecosystem. Redux is actually view-layer agnostic, so it can easily be used with Vue via simple bindingsState Management - 图5. 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.

Simple State Management from Scratch

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:

  1. const sourceOfTruth = {
  2. message: 'Hello'
  3. }
  4. const appA = Vue.createApp({
  5. data () {
  6. return sourceOfTruth
  7. }
  8. }).mount('#app-a')
  9. const appB = Vue.createApp({
  10. data () {
  11. return sourceOfTruth
  12. }
  13. }).mount('#app-b')

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.

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

  1. const store = {
  2. debug: true,
  3. state: {
  4. message: 'Hello!'
  5. },
  6. setMessageAction (newValue) {
  7. if (this.debug) {
  8. console.log('setMessageAction triggered with', newValue)
  9. }
  10. this.state.message = newValue
  11. },
  12. clearMessageAction () {
  13. if (this.debug) {
  14. console.log('clearMessageAction triggered')
  15. }
  16. this.state.message = ''
  17. }
  18. }

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.

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

  1. const appA = Vue.createApp({
  2. data () {
  3. return {
  4. privateState: {},
  5. sharedState: store.state
  6. }
  7. }
  8. }).mount('#app-a')
  9. const appB = Vue.createApp({
  10. data () {
  11. return {
  12. privateState: {},
  13. sharedState: store.state
  14. }
  15. }
  16. }).mount('#app-b')

State Management

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.

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 FluxState Management - 图7 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.

This brings us full circle back to vuexState Management - 图8, so if you’ve read this far it’s probably time to try it out!