Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- state: {
- users: [{name: 'Oscar', age: 23}, {name: 'Jens', age: 23}, {name: 'Olof', age: 25}]
- }
- // Vuex mutation
- // Vue uses dependency tracking via Object.defineProperty
- // so that components which are dependant on the state are automatically re-rendered.
- // No need for shouldComponentUpdate, immutability or full component vdom diffing
- incrementFirstAge(state) {
- // mutate state
- users[0].age++
- }
- // Redux reducer
- // State has to be immutable for React & the default shouldComponentUpdate to trigger.
- // It is equally or more expensive the "Vue way" and hands more responsibility to the developer
- // which has been abstracted & taken care of in the Vue case.
- incrementFirstAge(state) {
- // create state
- return {
- users: [
- ...state.users,
- {
- ...state.users[0],
- age: state.users[0].age + 1
- }
- ]
- }
- // alt. use something like Immutable but then it's still more "code" for the developer
- // to write and another, quite large, library to master.
- }
Add Comment
Please, Sign In to add comment