Skip to content

Instantly share code, notes, and snippets.

@MaraAlexa
Created January 9, 2018 09:17
Show Gist options
  • Save MaraAlexa/894a7e77a0a4a01b802655266d2b80d2 to your computer and use it in GitHub Desktop.
Save MaraAlexa/894a7e77a0a4a01b802655266d2b80d2 to your computer and use it in GitHub Desktop.
Action vs Mutations
// Mutations- the only way to MODIFY stATE - doesn't care about business logic, it just care about "state"
// are essentially events: each mutation has a name and a handler.
const store = new Vuex.Store({
state: {
count: 1
},
mutations: {
INCREMENT (state) {
// mutate state
state.count++
}
}
})
// Actions are just functions that dispatch mutations.- is business logic
// action can DISPATCH more than 1 MUTATION at a time,it just implement the business logic,
// it doesn't care about data changing (which manage by mutation)
// the simplest action
function increment (store) {
store.dispatch('INCREMENT')
}
// a action with additional arguments
// with ES2015 argument destructuring
function incrementBy ({ dispatch }, amount) {
dispatch('INCREMENT', amount)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment