Vuex 4 is in beta and it’s subject to change.
Vuex is a popular state management library for Vue.
Vuex 4 is the version that’s made to work with Vue 3.
In this article, we’ll look at how to use Vuex 4 with Vue 3.
Method-Style Getters
We can create getters that take one or more arguments.
To do that, we can write:
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://unpkg.com/vue@next"></script>
<script src="https://unpkg.com/vuex@4.0.0-beta.4/dist/vuex.global.js"></script>
<title>App</title>
</head>
<body>
<div id="app">
<div>
<p>{{getTodoById(1).text}}</p>
</div>
</div>
<script>
const store = new Vuex.Store({
state: {
todos: [
{ id: 1, text: "eat", done: true },
{ id: 2, text: "sleep", done: false }
]
},
getters: {
getTodoById: (state) => (id) => {
return state.todos.find((todo) => todo.id === id);
}
}
});
const app = Vue.createApp({
computed: {
...Vuex.mapGetters(["getTodoById"])
}
});
app.use(store);
app.mount("#app");
</script>
</body>
</html>
We created a store with the todos
state.
And we have the getTodosById
getter method which returns a function that takes the id
parameter and returns the state.todos
entry that matches the given id
value.
In the root Vue instance, we called Vuex.mapGetters
to map the getters to methods.
This way, we can call it in our component as we did in our template.
Mutations
We can use mutations to change the states in our store.
For example, we can write:
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://unpkg.com/vue@next"></script>
<script src="https://unpkg.com/vuex@4.0.0-beta.4/dist/vuex.global.js"></script>
<title>App</title>
</head>
<body>
<div id="app">
<button @click="decrement">decrement</button>
<p>{{count}}</p>
</div>
<script>
const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
decrement(state) {
state.count--;
}
}
});
const app = Vue.createApp({
methods: {
decrement() {
this.$store.commit("decrement");
}
},
computed: {
count() {
return this.$store.state.count;
}
}
});
app.use(store);
app.mount("#app");
</script>
</body>
</html>
We have the decrement
mutation that reduces state.count
‘s value by 1.
Then we can call the this.$store.commit
method to invoke our mutation to update the count
state.
We also have a computed count
property to get the value of the count
state.
Commit with Payload
We can create a mutation that takes a payload.
A mutation method accepts a 2nd parameter with the value.
For instance, we can write:
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://unpkg.com/vue@next"></script>
<script src="https://unpkg.com/vuex@4.0.0-beta.4/dist/vuex.global.js"></script>
<title>App</title>
</head>
<body>
<div id="app">
<button @click="decrement">decrement</button>
<p>{{count}}</p>
</div>
<script>
const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
decrement(state, n) {
state.count -= n;
}
}
});
const app = Vue.createApp({
methods: {
decrement() {
this.$store.commit("decrement", 5);
}
},
computed: {
count() {
return this.$store.state.count;
}
}
});
app.use(store);
app.mount("#app");
</script>
</body>
</html>
We added a n
parameter to the decrement
mutation method to subtract state.count
by n
.
Then we call commit
with a 2nd argument to pass in the value we want to subtract state.count
by.
Now when we click the decrement button, we can see the count
value displayed.
Conclusion
We can create mutations to modify state data with Vuex 4.
Getters can be defined as a method style getter to let us pass in arguments.