当前位置: 首页 > 知识库问答 >
问题:

未捕获类型错误:存储。getState不是一个函数

梅安平
2023-03-14

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

警告:失败的proType:无效的propstore类型的函数提供给提供者,预期的对象。检查Root的渲染方法。bundle.js:6169

警告:propType失败:提供给DevToolsRapper函数类型的存储无效,应为对象。检查Root的渲染方法。捆js:6169

警告:失败的上下文类型:类型函数的无效子上下文store提供给供应商,预期的对象。检查Root的渲染方法。bundle.js:6169

警告:失败的上下文类型:提供给Connect(AsyncApp)函数类型的存储无效,应为对象。检查提供程序的渲染方法。

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;
    }

在该错误发生之前的断点处,控制台。log(store)返回此值。

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

      var middlewareAPI = {
        getState: store.…

但是,当我使用节点后端、控制台运行完全相同的代码时。日志(存储)返回此对象。

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

我的根组件和配置存储。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>
);
}
}

配置tore.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);
}

共有1个答案

凌宏大
2023-03-14

我猜您正在运行一个较旧的示例代码。Redux 2.0compose()函数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);
 类似资料: