React store.getState is not a function

非 Y 不嫁゛ 提交于 2019-12-06 17:12:39

问题


Here is my code:

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

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

Any solution?


回答1:


Notice that in your Routes.js the store is not being initialized properly. You should add these lines:

  const initialState = {};
  const store = configureStore(initialState, browserHistory);

as in your index.js file.




回答2:


I was doing this (a dynamic require) ..

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

but for some reason when using require instead of import you have to do this ..

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



回答3:


This is a typo that generated the error: TypeError: store.getState is not a function

Wrong

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

Correct

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

Notice the added parenthesis () on applyMiddleware.




回答4:


Not sure if this will help but you named your import { Providers } instead of { Provider } from react-redux library.



来源:https://stackoverflow.com/questions/41962033/react-store-getstate-is-not-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!