Skip to content

Instantly share code, notes, and snippets.

@vjk2005
Forked from anonymous/counter.js
Last active June 22, 2016 21:23
Show Gist options
  • Save vjk2005/e4639224684d445b76cc59dc84387541 to your computer and use it in GitHub Desktop.
Save vjk2005/e4639224684d445b76cc59dc84387541 to your computer and use it in GitHub Desktop.
/** @jsx React.createElement */
/* ^ JS bin needs this or it will start complaining */
/* Redux code to create a counter. Needs a div with ID "app" to start working */
const counter = (state = 0, action) => {
switch(action.type) {
case 'INCREMENT':
return state + 1
case 'DECREMENT':
return state - 1
default:
return state
}
}
const Counter = ({value, onIncrement, onDecrement}) => (
<div>
<h1>{value}</h1>
<button onClick={onIncrement}>+</button>
<button onClick={onDecrement}>-</button>
</div>
)
const {createStore} = Redux
const store = createStore(counter)
const render = () => {
ReactDOM.render(
<Counter
value = {store.getState()}
onIncrement = {() => store.dispatch({type: 'INCREMENT'})}
onDecrement = {() => store.dispatch({type: 'DECREMENT'})}
/>,
document.getElementById('app')
)
}
// start app
store.subscribe(render)
render() // first time needs manual inovacation, subsequent calls come automatically from the subscribe method above
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment