-
-
Save SRatna/507d0afa56641f6495d16d88e3be6df9 to your computer and use it in GitHub Desktop.
Redux reducer using object literal instead of a switch statement
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
| import {createStore} from 'redux'; | |
| const counter = (state = 0, action) => { | |
| const index = { | |
| 'INCREMENT': () => { | |
| return state+1; | |
| }, | |
| 'DECREMENT': () => { | |
| return state-1; | |
| }, | |
| 'DEFAULT': () => { | |
| return state; | |
| } | |
| }; | |
| return (index[action.type] || index['DEFAULT'])(); | |
| } | |
| const store = createStore(counter); | |
| store.subscribe( () => { console.log(store.getState()); } ); | |
| store.dispatch({ type: 'INCREMENT' }); | |
| store.dispatch({ type: 'DECREMENT' }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment