【问题标题】:redux store Error while creating store after installing redux-sagaredux store 安装 redux-saga 后创建 store 时出错
【发布时间】:2023-08-22 04:47:01
【问题描述】:

我想在我的项目中使用 redux-saga,安装 redux-saga 后,当我在 store.js 文件中进行更改时会报错

错误:您似乎将几个存储增强器传递给 createStore()。这是不支持的。相反,将它们组合成一个函数。

# store.js                     
import { createStore, applyMiddleware, compose } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import thunk from 'redux-thunk';
import rootReducer from './reducers'
import createSagaMiddleware from 'redux-saga';
import rootSaga from './actions/sagas';


const sagaMiddleware = createSagaMiddleware();

const composeEnhancers =
  typeof window === 'object' &&
  window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ?   
    window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
      // Specify extension’s options like name, actionsBlacklist, actionsCreators, serialize...
    }) : compose;


const enhancer = composeEnhancers(
  applyMiddleware(sagaMiddleware)
);



const initialState = {};

const middleware = [thunk];

const store = createStore(
  rootReducer,
  initialState,
  enhancer,
  composeWithDevTools(applyMiddleware(...middleware))
);
  


sagaMiddleware.run(rootSaga);
export default store; 

我对商店了解不多。请看看你能不能帮忙。

【问题讨论】:

    标签: reactjs redux react-redux redux-saga


    【解决方案1】:

    你正在编写你的开发工具和中间件两次,只使用两者之一。

    const store = createStore(
      rootReducer,
      initialState,
    // either this line
      enhancer,
    // or this line
      composeWithDevTools(applyMiddleware(...middleware))
    );
    

    另外,仅供参考:这是一种相当过时的 redux 风格。如果您现在正在学习 redux 并遵循这种方法,您将编写大量不必要的样板文件。请关注the official redux documentation 上的官方 redux 教程,而不是你现在正在学习的任何教程。

    【讨论】: