【问题标题】:Redux tool kit error Error: Before running a Saga, you must mount the Saga middleware on the Store using applyMiddlewareRedux 工具包报错Error: before running a Saga, you must mount the Saga middleware on the Store using applyMiddleware
【发布时间】:2021-10-03 08:33:07
【问题描述】:

我正在使用 redux 工具包并收到此错误,这是我的商店设置,

import {  configureStore } from '@reduxjs/toolkit';
import { getDefaultMiddleware } from '@reduxjs/toolkit';
import counterReducer from './SlicesAndReducers';
import createSagaMiddleware from "redux-saga";
import rootSagaToolKit from './Sagas/SagaIndex'
import { applyMiddleware } from 'redux';
const sagaMiddleware = createSagaMiddleware();

const store = configureStore({
  reducer: {
    counter: counterReducer,
    middleware: [...getDefaultMiddleware({ thunk: false }),sagaMiddleware],
  }
});
sagaMiddleware.run(rootSagaToolKit);

export default store;

我已经检查了 Stackoverflow ,但没有找到任何解决方案

【问题讨论】:

    标签: reactjs redux-thunk redux-toolkit


    【解决方案1】:

    这条线应该移出减速器对象..middleware: [...getDefaultMiddleware({ thunk: false }),sagaMiddleware],..检查我下面的示例:

    import { configureStore } from '@reduxjs/toolkit';
    import { persistStore } from 'redux-persist';
    import createSagaMiddleware from 'redux-saga';
    
    import persistedReducer from './rootReducer';
    import rootSaga from './rootSaga';
    
    const sagaMiddleware = createSagaMiddleware();
    
    export const store = configureStore({
      reducer: persistedReducer,
      middleware: getDefaultMiddleware =>
        getDefaultMiddleware({
          thunk: false,
          immutableCheck: false,
          serializableCheck: false
        }).concat(sagaMiddleware),
      devTools: process.env.NODE_ENV !== 'production'
    });
    
    export const persistor = persistStore(store);
    
    sagaMiddleware.run(rootSaga);
    

    【讨论】:

    • 是的,我之前找到了解决方案,但是谢谢,这也是添加中间件的正确方法。
    猜你喜欢
    • 1970-01-01
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    • 2021-03-17
    • 2013-09-30
    • 2019-12-10
    • 1970-01-01
    相关资源
    最近更新 更多