Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. ```js
  2. const createAction = (type) => {
  3. const actionCreator = (payload) => ({
  4. type,
  5. payload,
  6. })
  7.  
  8. actionCreator.type = type
  9.  
  10. return actionCreator
  11. }
  12.  
  13. const createReducer = (actionHandlers, initialState) => (
  14. (state = initialState, action) => {
  15. const handler = actionHandlers[action.type]
  16.  
  17. return handler ? handler(state, action) : state
  18. }
  19. )
  20. ```
  21.  
  22. usage:
  23.  
  24. ```js
  25. export const fetchMany = createAction(`foo/FETCH_MANY`)
  26.  
  27.  
  28. console.log(fetchMany.type) // 'foo/FETCH_MANY'
  29. console.log(fetchMany()) // {type: 'foo/FETCH_MANY', payload: null}
  30.  
  31. // dispatch(fetchMany(payload))
  32.  
  33.  
  34. const actionHandlers = {
  35. [fetchMany.type]: (state, action) => ({ ...state, ...action.payload })
  36. }
  37.  
  38. const initialState = {}
  39.  
  40. const fooReducer = createReducer(actionHandlers, initialState)
  41.  
  42. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement