Uncaught TypeError: store.getState is not a function

Uncaught TypeError: store.getState is not a function

在我的 redux js 应用程序(google appengine 后端)中,当包含 Root 组件的页面打开时,我收到以下警告和错误消息。

警告:propType 失败:提供给 Providerfunction 类型的无效道具 store,应为 object。检查 Root 的渲染方法。 bundle.js:6169

警告:propType 失败:提供给 DevToolsWrapperfunction 类型的无效道具 store,应为 object。检查 Root 的渲染方法。 bundle.js:6169

警告:上下文类型失败:无效的子上下文 store 类型 function 提供给 Provider,应为 object。检查 Root 的渲染方法。 bundle.js:6169

警告:上下文类型失败:提供给 Connect(AsyncApp) 的类型 function 的上下文 store 无效,应为 object。检查 Provider.

的渲染方法
Uncaught TypeError: store.getState is not a function

这里是react-redux的lib/components/createConnect.js.

报错的地方
    function computeStateProps(store, props) {
      var state = store.getState();  <<<<< The error occurs here.
      var stateProps = shouldUpdateStateProps ? finalMapStateToProps(state, props) : finalMapStateToProps(state);

      _invariant2['default'](_utilsIsPlainObject2['default'](stateProps), '`mapStateToProps` must return an object. Instead received %s.', stateProps);
      return stateProps;
    }

在断点处,就在这个错误发生之前,console.log(store) returns this.

function (reducer, initialState) {
      var store = next(reducer, initialState);
      var _dispatch = store.dispatch;
      var chain = [];

      var middlewareAPI = {
        getState: store.…

但是,当我 运行 使用 Node 后端的完全相同的代码时,console.log(存储)returns 这个对象。

devToolsStore: Object
dispatch: (action)
getReducer: getReducer()
getState: getState()
replaceReducer: replaceReducer(nextReducer)
subscribe: subscribe(listener)

我的根组件和 configureStore.js 看起来像这样:

import React, { Component } from 'react';
import { Provider } from 'react-redux';
import configureStore from '../store/configureStore';

import { createStore, applyMiddleware, combineReducers, compose } from 'redux';
import { DevTools, DebugPanel, LogMonitor } from 'redux-devtools/lib/react';


import AsyncApp from './AsyncApp';

const store = configureStore();

export default class Root extends Component {
  render() {
return (
 <div>
  <Provider store={store}>
    {() => <AsyncApp />}
  </Provider>
  <DebugPanel top right bottom>
    <DevTools store={store} monitor={LogMonitor} />
  </DebugPanel>
</div>
);
}
}

configureStore.js

import { createStore, applyMiddleware, combineReducers, compose } from "redux";
import thunkMiddleware from "redux-thunk";
import loggerMiddleware from "redux-logger";
import rootReducer from "../reducers";
import thunk from 'redux-thunk';
import { devTools, persistState } from 'redux-devtools';

const finalCreateStore = compose(
    applyMiddleware(thunkMiddleware),
    devTools(),
    persistState(window.location.href.match(/[?&]debug_session=([^&]+)\b/)),
    createStore
);

export default function configureStore(initialState) {
  return finalCreateStore(rootReducer, initialState);
}

我猜您是 运行 较旧的示例代码。有一个 breaking change in Redux 2.0 compose() function API.

而不是

const finalCreateStore = compose(
    applyMiddleware(thunkMiddleware),
    devTools(),
    persistState(window.location.href.match(/[?&]debug_session=([^&]+)\b/)),
    createStore
);

你可能想要

const finalCreateStore = compose(
    applyMiddleware(thunkMiddleware),
    devTools(),
    persistState(window.location.href.match(/[?&]debug_session=([^&]+)\b/))
)(createStore);