Skip to content

Instantly share code, notes, and snippets.

@j-hannes
Last active May 22, 2016 16:10
Show Gist options
  • Save j-hannes/3ca760ad17c78411bc814fc8f2ff456f to your computer and use it in GitHub Desktop.
Save j-hannes/3ca760ad17c78411bc814fc8f2ff456f to your computer and use it in GitHub Desktop.
redux counter in es6
import React from 'react'
import ReactDOM from 'react-dom'
import { createStore } from 'redux'
const INCREMENT = 'INCREMENT'
const DECREMENT = 'DECREMENT'
const reducer = (state = 0, action) => {
switch (action.type) {
case INCREMENT:
return state + 1
case DECREMENT:
return state - 1
default:
return state
}
}
const store = createStore(reducer)
const Counter = ({value}) =>
<div>
<button onClick={() => store.dispatch({ type: DECREMENT })}>-</button>
<span>{value}</span>
<button onClick={() => store.dispatch({ type: INCREMENT })}>+</button>
</div>
const appContainer = document.getElementById('app')
const render = () =>
ReactDOM.render(
<Counter value={store.getState()} />,
appContainer
)
store.subscribe(render)
render()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment