Guest User

Untitled

a guest
Feb 19th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. /**
  2. * Creates a reducer with generic reducer logic handle for you.
  3. * For any cases not matched from an `action.type`, the passed state is returned.
  4. *
  5. * @author Gabe M.
  6. * @param {string} name - Name of the Reducer. Redux will use this for store and serialization
  7. * @param {Object} initialState - Initial State Object
  8. * @param {Object} cases - Cases is an object with describe your mutations.
  9. * Key is a matching action.type from a dispatched action.
  10. * Value is a function that accepts state, and action. This should return new non mutated state.
  11. *
  12. *
  13. * @example
  14. * createReducer('myReducer', { name: 'gabe' }, {
  15. * 'UPDATE_NAME': (state, action) => ({
  16. * ...state,
  17. * name: action.payload,
  18. * })
  19. * })
  20. *
  21. *
  22. * @return {function(state, action)} genericReducer - Returns a Reducer function
  23. */
  24. function createReducer(name, initialState, cases) {
  25. function genericReducer(state = initialState, action) {
  26. if (action.type in cases) {
  27. return cases[action.type](state, action);
  28. }
  29.  
  30. return state;
  31. }
  32.  
  33. genericReducer.reducerName = name;
  34.  
  35. return genericReducer;
  36. }
Add Comment
Please, Sign In to add comment