Created
November 26, 2015 01:29
-
-
Save kenichi-shibata/1de7cba2092d0c456f5e to your computer and use it in GitHub Desktop.
Redux Counter using store.dispatch store.subscribe (createStore) https://jsbin.com/voyehoq/edit?html,js,output
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
const counter = (state = 0, action) => { | |
switch(action.type){ | |
case 'INCREMENT': | |
return state + 1; | |
case 'DECREMENT': | |
return state - 1; | |
default: | |
return state; | |
} | |
}; | |
//created a basic counter using redux | |
const { createStore } = Redux; | |
// var createStore = Redux.createStore; //es5 | |
// import { createStore } from 'redux' //babel import | |
const store = createStore(counter); | |
// created a new reducer using pure function counter | |
const render = () => { | |
document.body.innerText = store.getState(); | |
}; | |
// get the state and put it in html body using render function | |
store.subscribe(render); | |
render(); | |
// subscribe to render and run it once to get the initial state | |
document.addEventListener('click', () => { | |
store.dispatch({type: 'INCREMENT'}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment