Last active
May 4, 2017 11:21
-
-
Save kishanio/17e4e025b8991796569bd6a924b8bb07 to your computer and use it in GitHub Desktop.
Redux : Store
This file contains 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
// 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