AllenYuan

reducer template

Apr 27th, 2020
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // template reducer configured by action type, state
  2. export default (type, initialData) => (state = {
  3.   loading: true,
  4.   data: initialData || [],
  5. }, action) => {
  6.   switch (action.type) {
  7.     // request => trigger loading state
  8.     case `REQUEST/${type}`:
  9.       return {
  10.         ...state,
  11.         loading: true,
  12.       };
  13.     // ok => put data in state
  14.     case `OK/${type}`:
  15.       return {
  16.         ...state,
  17.         loading: false,
  18.         data: action.payload,
  19.         error: false,
  20.       };
  21.     // error => put error in state
  22.     case `ERROR/${type}`:
  23.       return {
  24.         ...state,
  25.         error: action.error || true,
  26.         loading: false,
  27.       };
  28.     default:
  29.       return state;
  30.   }
  31. };
Advertisement
Add Comment
Please, Sign In to add comment