Skip to content

Instantly share code, notes, and snippets.

@andresilveirah
Created February 22, 2016 14:01
Show Gist options
  • Save andresilveirah/4c6483dc150a3d19f55f to your computer and use it in GitHub Desktop.
Save andresilveirah/4c6483dc150a3d19f55f to your computer and use it in GitHub Desktop.
A (ultra) simple Counter app using Redux and React.
import {createStore} from 'redux';
import React from 'react';
import ReactDOM from 'react-dom';
// the reducer is a pure function which is the responsible for changing
// the state of the application (without mutating the parameters)
const counter = (state = 0, action) => {
switch (action.type) {
case 'INCREMENT':
return state + 1;
case 'DECREMENT':
return state - 1;
default:
return state;
}
};
const store = createStore(counter);
// Creates a React component as a function.
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('app')
);
};
store.subscribe(render);
render();
// It's worth noting that the size of my bundle file increased from ~1kB when
// implementing the store myself to ~2kB when using Redux's store and then
// finally to ~700kB when adding React + ReactDOM packages.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment