A new Vue.js

State Management 🚀

@sum.cumo/vue-states
@sum.cumo/vue-history

Johannes Lamberts @jlbrts

JS Developer @sum.cumo

Why one more?

Why? 😳

Experience...

...building complex apps

  • + 300 single-file-components
  • + 140 routes
  • SSR
  • vue-apollo

Current Issues in Vuex

  • API size
  • Local state support
  • Analytics
  • Refactoring

Refactoring & Vuex

export default {

  computed: { 
    value() { 
      return this.$store.state.someValue
    },
  },

  methods: {
    update(nextValue) { 
      this.$store.commit('namespace/updateValue', nextValue)
    },
  },

}

How?

Principles

  1. Renderless Vue Components hold the state
  2. State can be provided from and injected
    into any component along the tree

Let's see some code

1. Install

npm install \
  @sum.cumo/vue-states \
  --save
import VueStates from '@sum.cumo/vue-states'

Vue.use(VueStates)

2. Define

export default {
  name: 'Warehouse',

  data() {
    return { 
      products: [],
    }
  },

  computed: { 
    productsCount() { 
      return this.products.length 
    },
  },

  methods: {
    async loadProducts() { 
      this.saveProducts(
        await this.$apollo.query({ query: productsQuery })
      )
    },
    saveProducts(products) {
      this.products = products
    }
  },
}

3. Provide

<template>
  <div>
    Product count: {{ Warehouse.productsCount }}
    <warehouse-list />
  </div>
</template>

<script>
import Warehouse from '@/models/Warehouse'

export default {
   name: 'WarehouseView',

   models: {
      Warehouse,
   },
}

</script>

4. Inject

<template>
   <section>
      <div
         v-for="product of Warehouse.products"
         :key="product.id"
      >
         {{ product.count }} x {{ product.label }}
         <button @click="Warehouse.incoming(product.id)">
            Add
         </button>
         <button @click="Warehouse.outgoing(product.id)">
            Remove
         </button>
      </div>
   </section>
</template>

<script>
export default {
  name: 'WarehouseList',

  injectModels: [
    'Warehouse',
  ],
}
</script>

Recap

Demo

Features

  • Eased learning
  • Eased refactoring
  • Native local state, still hydratable
  • Natural integration
    e.g. access to $router, $apollo
  • Simplified Lazy-Loading
    & Hot module replacement
  • Enhanced analytics with VueHistory

When to use?

@sum.cumo/vue-states

state management

Johannes Lamberts @jlbrts

JS Developer @sum.cumo

Thank you!

https://medium.com/@jlbrts/introducing-a-new-state-management-for-vue-js-b85be00b3299

https://slides.com/jlbrts/vue-state-management

https://github.com/JohannesLamberts/jsunconf-2019-vue-state-demo

A new Vue.js State Management

By Johannes Lamberts

A new Vue.js State Management

Do you use Vuex and love it for its simplicity? Do you build complex applications and have reached its limits? I’d like to present a new system that’s simpler to use and refactor – and yet one that supports even complex implementations: @sum.cumo/vue-models, and @sum.cumo/vue-history. Story: https://medium.com/@jlbrts/introducing-a-new-state-management-for-vue-js-b85be00b3299

  • 1,555