{{announcement.body}}
{{announcement.title}}

Reactive Vue 3 State

DZone 's Guide to

Reactive Vue 3 State

In this article, take a look at Reactive Vue 3 and see what makes a good application state.

· Web Dev Zone ·
Free Resource

Introduction

There's been a lot of discussion about state management in the upcoming Vue 3 framework. Some writers go as far as declaring Vuex dead. Reactivity is all we need, is the claim. Just like blockchain was supposed to cure all problems of modern civilization ;-) Jokes aside, it does look like an intriguing possibility, so we've taken a challenge and explored it in this article.

TL;DR

Vue 3 Reactivity System, now free of UI confines, can be efficiently employed as a powerful tool to handle state. This requires extra plumbing though, with no batteries included. If you want to get straight to technical details, scroll down to Reactive proposal for application state chapter below.

The source code for the article can be found at https://bitbucket.org/letsdebugit/vue-3-state. It uses the simple build-less architecture presented in my recent article about Vue JS 3 Application Without Build.

No Need for Vuex?

I've seen many brilliant proposals for managing state using Vue 3 reactivity system. Some would end with a bold conclusion: Vuex is no longer needed. On closer inspection I wasn't so sure. However ingenious, many proposals are essentially just another singleton with a bunch of methods, only made reactive. In all fairness this was perfectly doable with Vue 2 - yet we kept using Vuex. Why? Because there are deeper reasons why we got Vuex, Redux etc. Allow me now to stir a controversy:

A singleton with a bunch of methods is not a replacement for Vuex, even if it's reactive.

What Makes a Good Application State?

A viable solution for application state must include the following:

  • Centralized state available to all components. We used to call it a store.
  • Functions to safely modify the state, routinely called mutations.
  • Ability to modify state in async functions such as API calls. Vuex makes it possible through actions, Redux offers thunks.
  • Prevent programmer from shooting himself in the foot by modifying state directly.

It is the last requirement which lacks in majority of the proposals. And for me it is a crucial requirement.

All the effort of writing proper state and mutations is waste of time, if my colleagues are free to modify and hack state directly, causing inconsistencies and elusive bugs.

Vuex helps enforce this with strict mode, which throws exceptions on attempts to illegally alter the state. Redux resorts to immutability - any local alterations will simply be discarded with the next mutation cycle.

Finally, the mere fact of using Vuex (or Redux etc.) enforces structure and discipline. It does come with the inevitable boilerplate code. But then one simply has to follow the structure - design a state store, define state mutations, organize application logic into actions, create state getters when state gets complex etc.

It's this power of convention which makes your state easier for others to understand and use.

Those who worked on large Angular project without a third party state library immediately know what I'm talking about. Theoretically it is possible to manage state in Angular application using only Angular service and state-as-service pattern. But this is completely voluntary. There are no comprehensive guidelines and patterns of state management in official documentation. The inevitable outcome is a total mess of a state. Mysterious bugs and overwhelmed programmers eventually leaving the ship. Maybe others were luckier, but I have yet to see a large-scale pure Angular application with properly managed state.

That Vue and React both propose a ready pattern for state manegement, cannot be overestimated. Yes, it comes with quirks and boilerplate. But it gives us predictable architecture and much needed rigour. Properly used, it will prevent less experienced programmer from inadvertent or purposeful misbehaviour. Redux and Vuex have trained a whole generation of programmers into full awareness of how to properly manage application state.

Vuex, Redux and alikes are doing great job healing mental damage inflicted by Angular's state-is-not-our-problem policy. Yet so many people still keep asking: "What have the Romans ever done for us?"

Reactive Proposal for Application State

Having praised Vuex, let's try and come up with a simple reactive alternative which meets the criteria specified above. My additional goal is to have as little boilerplate as possible.

Usage

This is how I wish I could define a fictional state store representing user session:

JavaScript
 




x
21


 
1
import { createStore } from './vue-state.js'
2
 
           
3
export default createStore({
4
  userName: '',
5
  sessionToken: null,
6
 
           
7
  async login (userName, password) {
8
    await this.logout()
9
    this.sessionToken = await SessionAPI.validate(userName, password)
10
    this.userName = userName
11
  },
12
 
           
13
  async logout () {
14
    if (this.isLoggedIn) {
15
      await SessionAPI.invalidate(sessionToken)
16
    }
17
    this.userName = null
18
    this.sessionToken = null
19
  }
20
})
21
 
           


Apparently, the only boilerplate here is a call to createStore method. This is entirely in line with Vue 3 conventions, where we already have things like createApp or createRouter.

Here's how the store can be used in a component:

JavaScript
 




xxxxxxxxxx
1
14


1
import sessionStore from '../store/session.js'
2
 
           
3
export default {
4
  setup () {
5
    const { state, login, logout } = sessionStore
6
 
           
7
    return {
8
      state,
9
      login,
10
      logout
11
    }
12
  }
13
}
14
 
           


In HTML template we can refer to the store like this:

HTML
 




xxxxxxxxxx
1


 
1
<div v-if="state.userName">
2
  {{ state.userName }}
3
  <button @click="logout">Log out</button>
4
</div>


Additionally, when programmer attempts to alter state manually, I want the code to throw an exception:

JavaScript
 




xxxxxxxxxx
1


 
1
import sessionStore from '../store/session.js'
2
 
           
3
export default {
4
  setup () {
5
    const { state } = sessionStore
6
    // This must throw!
7
    state.userName = 'bob'
8
  }
9
}


Implementation

To achieve the above, actually very little code is needed. Thanks to the reactive magic of Vue 3, all it takes is 30 lines of code:

JavaScript
 




x


1
import { reactive, readonly, computed } from 'vue'
2
 
           
3
export function createStore (store = {}) {
4
  // Prepare state properties
5
  const entries = Object.entries(store)
6
  const state = reactive(entries
7
    .filter(entry => typeof entry[1] !== 'function')
8
    .reduce((all, [key, value]) => {
9
      all[key] = value
10
      return all
11
    }, {})
12
  )
13
 
           
14
  // Prepare mutation functions, make the state available in their body through `this`
15
  const mutations = entries
16
    .filter(entry => typeof entry[1] === 'function')
17
    .reduce((all, [name, handler]) => {
18
      all[name] = (...args) => handler.call(state, ...args)
19
      return all
20
    }, {})
21
 
           
22
  // Add mutations to state object, so that mutations can call each other through `this`
23
  for (const [name, mutation] of Object.entries(mutations)) {
24
    state[name] = mutation
25
  }
26
 
           
27
  return {
28
    ...mutations,
29
 
           
30
    // Return state as readonly, so the outside world cannot alter the state directly.
31
    get state () {
32
      return readonly(state)
33
    }
34
  }
35
}
36
 
           


Next Steps

An important addition in my proposal is extract method returned with the store. It is similar to Vuex mapState, and it can be used to extract values from state - either direct state properties or state-based expressions, for example:

JavaScript
 




x


 
1
import sessionStore from '../store/session-store.js'
2
 
           
3
const { sessionToken, userName } = sessionStore.extract('sessionToken', 'userName')
4
 
           
5
const { userNameCaps } = sessionStore.extract({
6
  userNameCaps: state => (state.userName || '').toUpperCase()
7
})


You might ask, why not simply use destructuring, for example:

JavaScript
 




xxxxxxxxxx
1


 
1
import { state } from '../store/session-store.js'
2
 
           
3
const { sessionToken, userName } = state


This will work, but with a big caveat. The returned values will not be reactive. If you try use them in data bindings, you won't see any changes in the UI, when their value changes. This is nothing unexpected to Vue 3 experts. It's the same behaviour as when extracting values from props object in setup method. Vue 3 documentation at https://composition-api.vuejs.org/api.html#setup explicitly warns:

Do NOT destructure the props object, as it will lose reactivity.

This is where our extract method helps. It not only extracts the required values from the state, but it also wraps them as computed. This way they can be safely used in data bindings or other computed expressions. Of course, if you don't need reactivity in a particular scenario, you will be fine with simple destructuring.

Please refer to the source code at https://bitbucket.org/letsdebugit/vue-3-state for implementation details.

Sample Application

We've provided a sample application where the proposed solution for state management has been employed. To make it less trivial than the usual single-button click-me application, it has the following features:

  • Two views and UI router
  • Reusable components
  • Two state stores - greetingStore and sessionStore, used by the views and components as the ultimate source of truth

You can run the sample application here.

It uses a simple architecture presented in my article about Vue JS 3 Application Without Build. To run it, you don't need npm or webpack or anything else than a modern browser. Serve the source folder with something like serve or python -m SimpleHTTPServer 8000 and there it is!

Summary

We have come up with a simple solution for managing state which seems to meet the basic criteria for a state store. It shows clearly that Vue 3 Reactivity System can be indeed employed as a tool to handle state in Vue 3 applications. Hopefully we'll be able to deploy it soon as a reusable npm package!

References

The article is also available at my blog Let's Debug It.

The complete source code can be found at https://bitbucket.org/letsdebugit/vue-3-state. Feel free to clone and reuse this code. Any suggestions or questions are most welcome!

I'd like to thank Jake Whelan for inspiration and valuable insights. And the usual credits and thanks to the creators of the awesome Vue JS framework.

Topics:
design pattens, javascript, state management, vue, vuejs, web application architecture, web development

Published at DZone with permission of Tomasz Waraksa . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}