2015-10-01 74 views
6

在我的redux js應用程序(google appengine後端)中,當打開包含Root組件的頁面時,出現以下警告和錯誤消息。Uncaught TypeError:store.getState不是函數

警告:失敗propType:無效道具store類型function提供給Provider,預計爲object。檢查渲染方法Root。 bundle.js:6169

警告:無法propType:供給到DevToolsWrapperfunction類型的無效丙store,預期object。檢查渲染方法Root。 bundle.js:6169

警告:無法上下文類型:提供給Providerfunction型的無效子Context store,預計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)返回此值。但是,當我使用Node後端運行完全相同的代碼時,console.log(store)會返回此對象。

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

回答

8

我的猜測是,你正在運行一個較舊的示例代碼。有一個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); 
相關問題