Created
February 11, 2018 02:06
-
-
Save mklemersson/91526facab34212140c788a8a19adb2c to your computer and use it in GitHub Desktop.
Simple React Counter
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
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') | |
); | |
}; | |
store.subscribe(render); | |
render(); | |
// a few test to be used with expect.js | |
// expect( | |
// counter(0, { type: 'TEST' }) | |
// ).toEqual(0); | |
// expect( | |
// counter(0, { type: 'INCREMENT' }) | |
// ).toEqual(1); | |
// expect( | |
// counter(1, { type: 'DECREMENT' }) | |
// ).toEqual(0); | |
// console.log('Tests passed'); |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
<script src="https://fb.me/react-15.1.0.js"></script> | |
<script src="https://fb.me/react-dom-15.1.0.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.7.2/redux.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/expect/1.20.2/expect.js"></script> | |
</head> | |
<body> | |
<div id="app"></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment