AllenYuan

router - refactor

Apr 24th, 2020
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // redux utilities
  2. // create a store, apply middlewares to it
  3. import { createStore, applyMiddleware } from 'redux';
  4. // middleware to parse actions' promises
  5. import { promiseMiddleware } from './middleware';
  6.  
  7. // defaultstate + trivial reducer
  8. const defaultState = {
  9.   appName: 'conduit',
  10.   articles: null,
  11. };
  12. const reducer = function(state = defaultState, action) {
  13.   // update state based on the action type
  14.   switch (action.type) {
  15.     // HOME_PAGE_LOADED => put articles into state
  16.     case 'HOME_PAGE_LOADED':
  17.       return { ...state, articles: action.payload.articles };
  18.     // unrecognized action type => don't change state
  19.     default:
  20.       return state;
  21.   }
  22. };
  23.  
  24. const middleware = applyMiddleware(promiseMiddleware);
  25.  
  26. // create store
  27. const store = createStore(reducer, middleware);
  28.  
  29. export default store;
Advertisement
Add Comment
Please, Sign In to add comment