Skip to content

Instantly share code, notes, and snippets.

@kenichi-shibata
Created November 26, 2015 03:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenichi-shibata/2f1b75fca7d45f667b26 to your computer and use it in GitHub Desktop.
Save kenichi-shibata/2f1b75fca7d45f667b26 to your computer and use it in GitHub Desktop.
Simple Counter Using React Redux
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://fb.me/react-0.14.0.js"></script>
<script src></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.0.4/redux.min.js"></script>
<script src="https://fb.me/react-dom-0.14.0.js
"></script>
<title>React-Redux First Touch :)</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
const counter = (state = 0, action) => {
switch(action.type){
case 'INCREMENT':
return state + 1;
case 'DECREMENT':
return state - 1;
default:
return state;
}
};
//created a basic counter using redux
const { createStore } = Redux;
// same as
// var createStore = Redux.createStore; //es5
// import { createStore } from 'redux' //babel import
const store = createStore(counter);
// created a new reducer using pure function counter
//
// creating a react dom component Counter <Counter />
const Counter = ({
value,
onIncrement,
onDecrement
}) => (
<div>
<h1>{value}</h1>
<button onClick={onIncrement}>+</button>
<button onClick={onDecrement}>-</button>
</div>
);
const render = () => {
ReactDOM.render(<Counter value={store.getState()}
onIncrement={() =>
store.dispatch({
type : 'INCREMENT'
})
}
onDecrement={() =>
store.dispatch({
type : 'DECREMENT'
})
}
/>,
document.getElementById('root')
);
};
store.subscribe(render);
render();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment