【问题标题】:redux saga cannot read property getState of undefinedredux saga 无法读取未定义的属性 getState
【发布时间】:2025-12-26 06:25:12
【问题描述】:

我正在尝试使用 saga 中间件和异步操作以及 react-router-redux 和选择器建立一个 react redux 存储。

对于某些 sagamiddleware 无法读取未定义的属性 getState。我不知道为什么。

我的*目录结构是:

./src/app.js
./src/sagas/index.js and imported sagas into this file
./src/Containers/App/actions.js constants.js appReducer.js selectors.js
./src/Containers/App/toplevelcomponenthere

app.js

import sagas from './sagas';
import createReducer from './reducers.js';

const sagaMiddleware = createSagaMiddleware();
const createStoreWithMiddleware = applyMiddleware(reduxRouterMiddleware,
sagaMiddleware(...sagas))(createStore);

const store = createStoreWithMiddleware(createReducer);
const reduxRouterMiddleware = syncHistoryWithStore(browserHistory)

//add selectors and history
import { selectLocationState } from './Containers/App/selectors';
const history = syncHistoryWithStore(browserHistory, store, {
  selectLocationState: selectLocationState(),
});

class Base extends Component {
  render() {
    return (
    <Provider store={store}>
    {children}
    </Provider>
    )
  }
}

【问题讨论】:

标签: javascript reactjs redux redux-saga


【解决方案1】:

您以错误的方式应用 saga 中间件。看看example sagaMiddlewate 不带任何参数。

您必须在创建商店后将root saga 传递给store.runSaga 函数,例如here。这就是 root saga - 单个函数,例如 fork 您想要使用的其他 saga。我认为您从 ./sagas 导入的 sagas 不是一个函数,因为您已对其应用了扩展运算符。

【讨论】:

    最近更新 更多