Skip to content

Instantly share code, notes, and snippets.

@twist900
Last active August 22, 2016 07:12
Show Gist options
  • Save twist900/2835fdf90a8c3f4b203bf1f179f9e5a3 to your computer and use it in GitHub Desktop.
Save twist900/2835fdf90a8c3f4b203bf1f179f9e5a3 to your computer and use it in GitHub Desktop.
A simplest redux example
#Reducer
const counter = (state = 0, action)=> {
switch(action.type){
case "INCREMENT":
return state + 1;
case "DECREMENT":
return state - 1;
default:
return state;
}
}
const { createStore } = Redux;
#Store defined by passing a reducer
const store = createStore(counter);
const render = () => {
document.body.innerText = store.getState();
}
store.subscribe(render);
render();
#the state is updated through the use of actions
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