A look at state management on the web.
The state is all of the information that is retain by your application, often with respect to previous events or interactions.
State management makes the state of your application tangible in the form of a data structure that you can read from and write to. It makes your invisible state clearly visible for you to work with.
State can be a messy part of application development, especially when there are a lot of user interactions to manage.
Data from stores are displayed in views.
When a view uses data from a store it must also subscribe to change events from that store.
Actions define the internal API of your application.
They capture the ways in which anything might interact with your application.
It is a self-contained app
export default {
computed: {
user() {
return this.$store.state.user;
}
}
}
import {mapState} from 'vuex';
export default {
computed: mapState({
user: state => state.user
})
}
import {mapState} from 'vuex';
export default {
computed: mapState({
user: state => state.user
})
}
import {mapState} from 'vuex';
export default {
computed: mapState({ user: 'user' })
}
import {mapState} from 'vuex';
export default {
computed: mapState(['user'])
}
import {mapState} from 'vuex';
export default {
computed: {
localComputed() { return 'something'; },
...mapState(['user']),
}
}
export default new Vuex.Store({
...,
getters: {
doneTodos: state => {
return state.todos.filter(todo => todo.done);
},
doneTodosCount: (state, getters) => {
return getters.doneTodos.length;
},
}
});