Skip to content

Instantly share code, notes, and snippets.

@masa7351
Created May 3, 2018 04:25
Show Gist options
  • Save masa7351/4a0e6831dfefccfabd0676ac635d5816 to your computer and use it in GitHub Desktop.
Save masa7351/4a0e6831dfefccfabd0676ac635d5816 to your computer and use it in GitHub Desktop.
Higher Order Functionを使用して、Reducerを作成するサンプル
// handersには(state, action) => stateのreducerが渡される
export function createReducer(initialState: Object, handlers: Object): Function {
return function reducer(state: Object = initialState, action: Object): Object {
if ({}.hasOwnProperty.call(handlers, action.type)) {
// [ ]はaction.typeをKeyにしたreducerを返却
return handlers[action.type](state, action);
}
return state;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment