Skip to content

Instantly share code, notes, and snippets.

@SRatna
Forked from sebjwallace/reduxObjectLiteral.js
Created June 22, 2018 04:16
Show Gist options
  • Select an option

  • Save SRatna/507d0afa56641f6495d16d88e3be6df9 to your computer and use it in GitHub Desktop.

Select an option

Save SRatna/507d0afa56641f6495d16d88e3be6df9 to your computer and use it in GitHub Desktop.
Redux reducer using object literal instead of a switch statement
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