Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```js
- const createAction = (type) => {
- const actionCreator = (payload) => ({
- type,
- payload,
- })
- actionCreator.type = type
- return actionCreator
- }
- const createReducer = (actionHandlers, initialState) => (
- (state = initialState, action) => {
- const handler = actionHandlers[action.type]
- return handler ? handler(state, action) : state
- }
- )
- ```
- usage:
- ```js
- export const fetchMany = createAction(`foo/FETCH_MANY`)
- console.log(fetchMany.type) // 'foo/FETCH_MANY'
- console.log(fetchMany()) // {type: 'foo/FETCH_MANY', payload: null}
- // dispatch(fetchMany(payload))
- const actionHandlers = {
- [fetchMany.type]: (state, action) => ({ ...state, ...action.payload })
- }
- const initialState = {}
- const fooReducer = createReducer(actionHandlers, initialState)
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement