Skip to content

Instantly share code, notes, and snippets.

@torgeir
Last active March 21, 2018 09:25
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 torgeir/4e3aecab117762455dfb4226dc90253f to your computer and use it in GitHub Desktop.
Save torgeir/4e3aecab117762455dfb4226dc90253f to your computer and use it in GitHub Desktop.
Minimal redux example
const initialState = Immutable.Map({ count: 42 });
const actions = {
click: function () {
return { type: "click" };
},
asyncClick: function () {
return function (dispatch) {
setTimeout(() => dispatch(actions.click()), 1000);
};
}
};
const reducer = function (state = initialState, action) {
switch (action.type) {
case "click":
return state.update("count", c => c + 1);
default:
return state;
}
};
const store = redux.createStore(reducer, redux.applyMiddleware(reduxThunk));
const { dispatch, getState, subscribe } = store;
const Comp = React.createClass({
render: function () {
const { dispatch, count } = this.props;
return <div>
<button onClick={ () => dispatch(actions.click()) }>+</button>
<button onClick={ () => dispatch(actions.asyncClick()) }>async +</button>
{ count }
</div>;
}
});
const render = function () {
ReactDOM.render(
<Comp count={ getState().get("count") }
dispatch={ dispatch } />, el);
};
subscribe(render);
render();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment