Last active
May 22, 2016 16:10
-
-
Save j-hannes/3ca760ad17c78411bc814fc8f2ff456f to your computer and use it in GitHub Desktop.
redux counter in es6
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
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