Skip to content

Instantly share code, notes, and snippets.

@arecvlohe
Last active September 2, 2017 15:57
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 arecvlohe/4cab56d099b715544dc46b399379f8fd to your computer and use it in GitHub Desktop.
Save arecvlohe/4cab56d099b715544dc46b399379f8fd to your computer and use it in GitHub Desktop.
A simpler Redux boilerplate
import { combineReducers, createStore } from "redux";
import Type from "union-type";
// MESSAGES
const Msg = Type({ INCREMENT: [], DECREMENT: [], DEFAULT: [] });
// UPDATE
const nextState = Msg.caseOn({
INCREMENT: state => state + 1,
DECREMENT: state => state - 1,
DEFAULT: state => state,
_: state => state
});
function counter(state = 0, { type = Msg.DEFAULT, payload = null }) {
if (typeof type === "string") return state;
return nextState(type, state);
}
// STORE
const store = createStore(
combineReducers({
counter
}),
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);
// EXPORTS - ACTIONS & STORE
export const add = () => store.dispatch({ type: Msg.INCREMENT });
export const subtract = () => store.dispatch({ type: Msg.DECREMENT });
export default store;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment