Why do we need middleware for async flow in Redux?

前端 未结 11 2410
忘了有多久
忘了有多久 2020-11-22 04:17

According to the docs, \"Without middleware, Redux store only supports synchronous data flow\". I don\'t understand why this is the case. Why can\'t the container component

11条回答
  •  日久生厌
    2020-11-22 04:50

    To use Redux-saga is the best middleware in React-redux implementation.

    Ex: store.js

      import createSagaMiddleware from 'redux-saga';
      import { createStore, applyMiddleware } from 'redux';
      import allReducer from '../reducer/allReducer';
      import rootSaga from '../saga';
    
      const sagaMiddleware = createSagaMiddleware();
      const store = createStore(
         allReducer,
         applyMiddleware(sagaMiddleware)
       )
    
       sagaMiddleware.run(rootSaga);
    
     export default store;
    

    And then saga.js

    import {takeLatest,delay} from 'redux-saga';
    import {call, put, take, select} from 'redux-saga/effects';
    import { push } from 'react-router-redux';
    import data from './data.json';
    
    export function* updateLesson(){
       try{
           yield put({type:'INITIAL_DATA',payload:data}) // initial data from json
           yield* takeLatest('UPDATE_DETAIL',updateDetail) // listen to your action.js 
       }
       catch(e){
          console.log("error",e)
         }
      }
    
    export function* updateDetail(action) {
      try{
           //To write store update details
       }  
        catch(e){
           console.log("error",e)
        } 
     }
    
    export default function* rootSaga(){
        yield [
            updateLesson()
           ]
        }
    

    And then action.js

     export default function updateFruit(props,fruit) {
        return (
           {
             type:"UPDATE_DETAIL",
             payload:fruit,
             props:props
           }
         )
      }
    

    And then reducer.js

    import {combineReducers} from 'redux';
    
    const fetchInitialData = (state=[],action) => {
        switch(action.type){
          case "INITIAL_DATA":
              return ({type:action.type, payload:action.payload});
              break;
          }
         return state;
      }
     const updateDetailsData = (state=[],action) => {
        switch(action.type){
          case "INITIAL_DATA":
              return ({type:action.type, payload:action.payload});
              break;
          }
         return state;
      }
    const allReducers =combineReducers({
       data:fetchInitialData,
       updateDetailsData
     })
    export default allReducers; 
    

    And then main.js

    import React from 'react';
    import ReactDOM from 'react-dom';
    import App from './app/components/App.jsx';
    import {Provider} from 'react-redux';
    import store from './app/store';
    import createRoutes from './app/routes';
    
    const initialState = {};
    const store = configureStore(initialState, browserHistory);
    
    ReactDOM.render(
           
                /*is your Component*/
           , 
    document.getElementById('app'));
    

    try this.. is working

提交回复
热议问题