Skip to content

Instantly share code, notes, and snippets.

@oliverturner
Created February 25, 2017 15:15
Show Gist options
  • Save oliverturner/c4dc5dfcefce0e251585ed8fafc15136 to your computer and use it in GitHub Desktop.
Save oliverturner/c4dc5dfcefce0e251585ed8fafc15136 to your computer and use it in GitHub Desktop.
switchless reducers
import addHandler from './helper'
import {UPDATE_PENDING, UPDATE_LOADED, UPDATE_FAILED} from './actions';
const initialState = {};
// Handlers
const onUpdatePending = (state) => state
const onUpdateLoaded = (state, json) => Object.assign({}, state, json);
const onUpdateFailed = (state, err) => state;
// Store
export default addHandlers(initialState, {
[UPDATE_PENDING]: onUpdatePending,
[UPDATE_LOADED]: onUpdateLoaded,
[UPDATE_FAILED]: onUpdateFailed
});
export default (initialState, handlers) =>
(state = initialState, action) =>
handlers[action.type]
? handlers[action.type](state, action.payload)
: state;
import { combineReducers } from 'redux';
import example from './example';
export default combineReducers({example});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment