繁体   English   中英

无法从我的 create-react-app 中的“redux-saga”导入 createSagaMiddleware

[英]Cannot import createSagaMiddleware from 'redux-saga' in my create-react-app

我的 create-react-app 中有以下导入:

import createSagaMiddleware from 'redux-saga';

问题是我的系统无法导入 createSagaMiddleware。

我正在运行版本:节点 12.13.0 npm 6.12.1

我的 package.json 看起来像这样:

{
  "name": "foo",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "firebase": "^7.1.0",
    "node-sass": "^4.12.0",
    "npm": "^6.12.1",
    "react": "^16.10.1",
    "react-dom": "^16.10.1",
    "react-redux": "^7.1.1",
    "react-router-dom": "^5.1.2",
    "react-scripts": "3.1.2",
    "react-stripe-checkout": "^2.6.3",
    "redux": "^4.0.4",
    "redux-logger": "^3.0.6",
    "redux-persist": "^6.0.0",
    "redux-saga": "^1.1.1",
    "redux-thunk": "^2.3.0",
    "reselect": "^4.0.0",
    "styled-components": "^4.4.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

我的 IDE 说(Intellij IDEA)说它“无法解析符号'createSagaMiddleware'。 我看到的实际错误消息是

Error: Actions must be plain objects. Use custom middleware for async actions

被扔在

componentDidMount() {
    const {fetchCollectionsStart} = this.props;
    fetchCollectionsStart();
};

->

const mapDispatchToProps = (dispatch) => ({
    fetchCollectionsStart: () => dispatch(fetchCollectionsStart())
});

export default connect(
    null,
    mapDispatchToProps
)(ShopPage);

fetchCollectionsStart 操作如下所示:

import {takeEvery} from 'redux-saga/effects';
import ShopActionTypes from "./shop.types";

export function* fetchCollectionsAsync() {
    yield console.log('I am fired');
}

export function* fetchCollectionsStart() {
    yield takeEvery(
        ShopActionTypes.FETCH_COLLECTIONS_START,
        fetchCollectionsAsync
    );
}

我的 redux 商店看起来像这样:

import { createStore, applyMiddleware } from 'redux';
import { persistStore } from 'redux-persist';
import logger from 'redux-logger';
import createSagaMiddleware from 'redux-saga';

import {fetchCollectionsStart} from "./shop/shop.sagas";

import rootReducer from './root-reducer';

const sagaMiddleware = createSagaMiddleware();

const middlewares = [sagaMiddleware];

if (process.env.NODE_ENV === 'development') {
    middlewares.push(logger);
}

export const store = createStore(rootReducer, applyMiddleware(...middlewares));

sagaMiddleware.run(fetchCollectionsStart);

export const persistor = persistStore(store);

export default { store, persistStore };

我在https://github.com/redux-saga/redux-saga/issues/1967看到了类似的问题。 然而,这个答案并不能在这里解决。

有任何想法吗?

谢谢

正如错误所说,动作必须是普通对象。 显然,您正在调度一个传奇而不是一个动作。

将您的mapDispatchToProps代码块替换为:

const mapDispatchToProps = (dispatch) => ({
    fetchCollectionsStart: () => {
       dispatch({ type: ShopActionTypes.FETCH_COLLECTIONS_START });
    },
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM