Skip to content

Instantly share code, notes, and snippets.

@kishanio
Last active May 4, 2017 11:21
Show Gist options
  • Save kishanio/17e4e025b8991796569bd6a924b8bb07 to your computer and use it in GitHub Desktop.
Save kishanio/17e4e025b8991796569bd6a924b8bb07 to your computer and use it in GitHub Desktop.
Redux : Store
// Dependency : http://redux.js.org/
const { createStore } = Redux;
const counter = ( state = 0, action) => {
switch( action.type ) {
case 'INCREMENT':
return state+1;
case 'DECREMENT':
return state-1;
// handles undefined action
default:
return state;
}
}
const persistedState = 5;
// Creating and passing reducer to store.
const store = createStore(counter, persistedState); //passing persistedState as second argument to store.
const render = () => {
// Get state returns the state of application
document.body.innerText = store.getState();
}
store.subscribe(render);
render();
document.addEventListener('click', () => {
// Dispactching action
store.dispatch({type: 'INCREMENT'});
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment