Skip to content

Instantly share code, notes, and snippets.

@kenichi-shibata
Created November 26, 2015 01:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenichi-shibata/1de7cba2092d0c456f5e to your computer and use it in GitHub Desktop.
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
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