Answer the question
In order to leave comments, you need to log in
How and when is it correct to use Vuex actions for API requests?
After reading several articles on Vuex and API calls, I came to the conclusion that API requests should be made inside Vuex actions (supposedly these are best practices). However, I don't understand at all whether this is always necessary. For clarity, I will give an example.
Task: get and display a list of users in the component.
Create a simple Vuex store:
export default new Vuex.Store({
state: {
users: []
},
getters: {
getUsers(state) {
return state.users;
}
},
actions: {
loadUsers({ commit }) {
getUsersAPICall().then(function(response) {
commit("saveUsers", response['data']);
})
},
},
mutations: {
saveUsers(state, users) {
state.users = users;
},
}
})
export default {
name: "Items",
computed: {
users() {
return this.$store.getters['getUsers'];
}
},
created() {
this.$store.dispatch('loadUsers');
}
}
actions: {
.....
addUser({ commit }) {
addUserAPICall().then(function(response) {
// TODO
})
},
},
Answer the question
In order to leave comments, you need to log in
came to the conclusion that API requests should be made inside Vuex actions (supposedly these are best practices). However, I don't understand if this is always necessary.
Well, you get an ID in the answer, don't you? So there shouldn't be any collisions. And updating the data ... Well, in any case, you need to do it, regardless of whether you added something or not, if the task requires it
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question