Javascript React store.getState 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41962033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
React store.getState is not a function
提问by ssuhat
Here is my code:
这是我的代码:
store.js
商店.js
import {createStore, applyMiddleware, compose} from 'redux';
import {fromJS} from 'immutable';
import {routerMiddleware} from 'react-router-redux';
import createSagaMiddleware from 'redux-saga';
import createReducer from './reducers';
const sagaMiddleware = createSagaMiddleware();
export default function configureStore(initialState = {}, history) {
// Create the store with two middlewares
// 1. sagaMiddleware: Makes redux-sagas work
// 2. routerMiddleware: Syncs the location/URL path to the state
const middlewares = [sagaMiddleware, routerMiddleware(history)];
const enhancers = [applyMiddleware(...middlewares)];
const store = createStore(createReducer, fromJS(initialState), enhancers);
// Extensions
store.runSaga = sagaMiddleware.run;
store.asyncReducers = {}; // Async reducer registry
return store;
}
Routes.js
路由.js
import React from 'react';
import {Route, Router, IndexRoute, browserHistory} from 'react-router';
import {syncHistoryWithStore} from 'react-router-redux';
import store from './store';
import Welcome from './containers/Welcome';
const history = syncHistoryWithStore(browserHistory, store);
const routes = (
<Router history={history}>
<Route path="/">
<IndexRoute component={Welcome} />
</Route>
</Router>
);
export default routes;
Index.js
索引.js
import React from 'react';
import ReactDOM from 'react-dom';
import {browserHistory} from 'react-router';
import { Providers } from 'react-redux';
import configureStore from './store';
import routes from './routes';
const initialState = {};
const store = configureStore(initialState, browserHistory);
ReactDOM.render(
<Provider store={store}>
{routes}
</Provider>, document.getElementById('main-content')
);
I can't find where the culprit is. I tried to debug it, but can't found what really make it those error. error: Uncaught TypeError: store.getState is not a function
我找不到罪魁祸首在哪里。我试图调试它,但找不到真正导致这些错误的原因。错误:未捕获的类型错误:store.getState 不是函数
Any solution?
有什么解决办法吗?
回答by dlopez
Notice that in your Routes.jsthe storeis not being initialized properly. You should add these lines:
请注意,您Routes.js的store未正确初始化。您应该添加这些行:
const initialState = {};
const store = configureStore(initialState, browserHistory);
as in your index.jsfile.
就像在您的index.js文件中一样。
回答by Steven
This is a typo that generated the error: TypeError: store.getState is not a function
这是一个产生错误的错字: TypeError: store.getState is not a function
Wrong
错误的
const store = createStore(()=>[], {}, applyMiddleware);
Correct
正确的
const store = createStore(()=>[], {}, applyMiddleware());
Notice the added parenthesis ()on applyMiddleware.
请注意添加的括号()上applyMiddleware。
回答by danday74
I was doing this (a dynamic require) ..
我正在这样做(动态要求)..
const store = require('../store/app')
state = store.getState()
but for some reason when using requireinstead of importyou have to do this ..
但是出于某种原因,在使用require而不是import您必须这样做时..
const store = require('../store/app')
state = store.default.getState()
回答by mcguiretj
Not sure if this will help but you named your import { Providers } instead of { Provider } from react-redux library.
不确定这是否会有所帮助,但您从 react-redux 库中命名了 import { Providers } 而不是 { Provider } 。

