React store.getState不是函数

时间:2017-01-31 16:03:09

标签: javascript reactjs redux-saga

这是我的代码:

store.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

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

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

我无法找到罪魁祸首。我试图调试它,但无法找到真正使它成为错误的东西。错误:未捕获TypeError:store.getState不是函数

任何解决方案?

6 个答案:

答案 0 :(得分:10)

请注意,Routes.js store未正确初始化 const initialState = {}; const store = configureStore(initialState, browserHistory); 。您应该添加以下行:

index.js

Uncaught ReferenceError: this is not defined文件一样。

答案 1 :(得分:5)

我这样做(动态要求)..

    const store = require('../store/app')
    state = store.getState()

但由于某些原因,使用require代替import时,您必须这样做..

    const store = require('../store/app')
    state = store.default.getState()

答案 2 :(得分:2)

不确定这是否有帮助,但您从react-redux库中将您的import {Providers}命名为{Provider}。

答案 3 :(得分:2)

希望它会有所帮助,但是在我的情况下,我收到此错误,因为我的商店如下所示,这是一个函数:

 const store = preloadedState => {
    let initialState={}
    //some code to modify intialState
   
    return createStore(reducer, initialState)
}

但是在index.js中,我将存储作为函数传递,而不是其返回的值。

错误

<Provider store={store}>
    <MyApp />
</Provider>

正确

<Provider store={store()}>
    <MyApp />
</Provider>

答案 4 :(得分:0)

这是产生错误的错字:TypeError:store.getState不是函数

错误

const store = createStore(()=> [],{},applyMiddleware);

正确

const store = createStore(()=> [],{},applyMiddleware());

  

请注意applyMiddleware上添加的括号()

答案 5 :(得分:0)

这就是解决方案,祝你好运?

import { applyMiddleware, combineReducers, createStore } from "redux";
import { composeWithDevTools } from "redux-devtools-extension";
import thunk from 'redux-thunk'

const reducer = combineReducers({
    products: []
})

const middleware = [thunk]

const store = createStore(
    reducer,
    composeWithDevTools(applyMiddleware(...middleware))
)

export default store