AllenYuan

http - middleware - integration

Apr 23rd, 2020
534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // import app component to render
  2. import App from './components/App';
  3. import ReactDOM from 'react-dom';
  4. import React from 'react';
  5.  
  6. // redux utilities
  7. // create a stpre, apply middlewares to it
  8. import { createStore, applyMiddleware } from 'redux';
  9. // store provider
  10. import {Provider} from 'react-redux';
  11. // middleware to parse actions' promises
  12. import { promiseMiddleware } from './middleware';
  13.  
  14. // defaultstate + trivial reducer
  15. const defaultState = {
  16.   appName: 'conduit',
  17.   articles: null,
  18. };
  19. const reducer = function(state = defaultState, action) {
  20.   // update state based on the action type
  21.   switch (action.type) {
  22.     // HOME_PAGE_LOADED => put articles into state
  23.     case 'HOME_PAGE_LOADED':
  24.       return { ...state, articles: action.payload.articles };
  25.     // unrecognized action type => don't change state
  26.     default:
  27.       return state;
  28.   }
  29. };
  30.  
  31. // create store
  32. const store = createStore(reducer, applyMiddleware(promiseMiddleware));
  33.  
  34. // wrap app with provider, render it in root element
  35. ReactDOM.render(
  36.   <Provider store={store}>
  37.     <App />
  38.   </Provider>,
  39.   document.getElementById('root')
  40. );
Advertisement
Add Comment
Please, Sign In to add comment