Created
February 14, 2019 13:54
-
-
Save loveyunk/58a42f0f596b72764ed41e9d34449362 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { createStore } = require('redux'); | |
const initialState = { | |
age: 21 | |
}; | |
const myReducer = (state = initialState, action) => { | |
let newState = { ...state }; | |
if (action.type === 'ADD') { | |
newState.age += 1; | |
} | |
if (action.type === 'SUBSTRACT') { | |
newState.age -= 1; | |
} | |
return newState; | |
}; | |
const store = createStore(myReducer); | |
store.subscribe(() => { | |
console.log('store change: ' + JSON.stringify(store.getState())); | |
}); | |
store.dispatch({ type: 'ADD' }); | |
store.dispatch({ type: 'ADD' }); | |
store.dispatch({ type: 'ADD' }); | |
store.dispatch({ type: 'SUBSTRACT' }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment