Actions
Actions are similar to mutations, the differences being that:
- Instead of mutating the state, actions commit mutations.
- Actions can contain arbitrary asynchronous operations.
Let’s register a simple action:
Action handlers receive a context object which exposes the same set of methods/properties on the store instance, so you can call to commit a mutation, or access the state and getters via context.state
and context.getters
. We can even call other actions with context.dispatch
. We will see why this context object is not the store instance itself when we introduce Modules later.
In practice, we often use ES2015 to simplify the code a bit (especially when we need to call commit
multiple times):
actions: {
increment ({ commit }) {
commit('increment')
}
}
store.dispatch('increment')
This may look silly at first sight: if we want to increment the count, why don’t we just call store.commit('increment')
directly? Remember that mutations have to be synchronous. Actions don’t. We can perform asynchronous operations inside an action:
Actions support the same payload format and object-style dispatch:
// dispatch with a payload
store.dispatch('incrementAsync', {
amount: 10
})
// dispatch with an object
store.dispatch({
type: 'incrementAsync',
amount: 10
A more practical example of real-world actions would be an action to checkout a shopping cart, which involves calling an async API and committing multiple mutations:
checkout ({ commit, state }, products) {
// save the items currently in the cart
const savedCartItems = [...state.cart.added]
// send out checkout request, and optimistically
// clear the cart
commit(types.CHECKOUT_REQUEST)
// the shop API accepts a success callback and a failure callback
shop.buyProducts(
products,
// handle success
() => commit(types.CHECKOUT_SUCCESS),
// handle failure
() => commit(types.CHECKOUT_FAILURE, savedCartItems)
)
}
}
Note we are performing a flow of asynchronous operations, and recording the side effects (state mutations) of the action by committing them.
Actions are often asynchronous, so how do we know when an action is done? And more importantly, how can we compose multiple actions together to handle more complex async flows?
The first thing to know is that store.dispatch
can handle Promise returned by the triggered action handler and it also returns Promise:
actions: {
return new Promise((resolve, reject) => {
commit('someMutation')
resolve()
}, 1000)
})
}
}
Now you can do:
store.dispatch('actionA').then(() => {
// ...
})
And also in another action:
// assuming `getData()` and `getOtherData()` return Promises
actions: {
async actionA ({ commit }) {
commit('gotData', await getData())
},
async actionB ({ dispatch, commit }) {
await dispatch('actionA') // wait for `actionA` to finish
commit('gotOtherData', await getOtherData())
}
}