Created
May 3, 2018 04:25
-
-
Save masa7351/4a0e6831dfefccfabd0676ac635d5816 to your computer and use it in GitHub Desktop.
Higher Order Functionを使用して、Reducerを作成するサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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