Created
December 2, 2016 08:33
-
-
Save aamnah/f9c533fe0d02841b0ffdf390f4969e8d to your computer and use it in GitHub Desktop.
Basic Redux functions
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
| // define a reducer | |
| const reducer = function(state, action) { | |
| if (action.type === 'INC') { | |
| return state + action.payload | |
| } else if (action.type === 'DEC') { | |
| return state - action.payload | |
| } | |
| return state | |
| } | |
| // create the store, pass it the reducer | |
| const store = createStore(reducer, 0) | |
| // Subscribe to store changes | |
| store.subscribe(() => { | |
| console.log('store changed', store.getState()) | |
| }) | |
| // Send actions | |
| store.dispatch({ | |
| type: 'INC', | |
| payload: 199 | |
| }) | |
| store.dispatch({ | |
| type: 'DEC', | |
| payload: 575658 | |
| }) | |
| store.dispatch({ | |
| type: 'INC', | |
| payload: 191027 | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment