Guest User

Untitled

a guest
Nov 13th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. /**
  2. * Higher-order reducer factory for creating named reducers.
  3. * Both params are required, and it will return your reducer or state.
  4. *
  5. * @param key
  6. * @param reducer
  7. * @returns {function}
  8. */
  9. const createNamedReducer = (key, reducer) => {
  10. return (state, action) => {
  11. // Action must have a key called 'reducerName'. Use the optional metadata param with createAction
  12. const { reducerName } = action;
  13. const isInitialCall = state === undefined; // Check if initialization is happening
  14.  
  15. // If the provided key doesn't match the reducerName, and it's not the initialization, return the state
  16. if (reducerName !== key && !isInitialCall) {
  17. return state;
  18. }
  19.  
  20. // Otherwise return the reducer
  21. return reducer(state, action);
  22. };
  23. };
  24.  
  25. export default createNamedReducer;
Add Comment
Please, Sign In to add comment