Reputation: 454
i'm trying to move everything that API related to Vuex, however there are some cases that i don't really need to mutate anything after calling an action, is it ok? what is the best practices?
actions: {
save (context, payload) {
axios.post(`http://jsonplaceholder.typicode.com/posts`, {body: payload})
}
}
Upvotes: 1
Views: 114
Reputation: 992
If you just need to post the information to the API and make no changes to the state, then I would say it's almost okay!
But, if you are not changing any state, how the application change it's content to let the user know the API post worked or failed?
Best practice would be to mutate state upon the axios promise resolve
or fail
actions: {
save (context, payload) {
axios.post(`http://jsonplaceholder.typicode.com/posts`, {body: payload})
.then(data => /*change state to let the user that it succeed*/)
.catch(err => /*set a state variable that contains an error message*/);
}
}
Upvotes: 2