Last active
August 22, 2016 07:12
-
-
Save twist900/2835fdf90a8c3f4b203bf1f179f9e5a3 to your computer and use it in GitHub Desktop.
A simplest redux example
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
#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