25

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?

ssuhat
  • 6,243
  • 14
  • 50
  • 97
  • Is there a reason `enhancers` is an array? Also you need to initialize your store in the routes file. – hunterc Jan 31 '17 at 16:59

5 Answers5

18

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.

Matt
  • 2,910
  • 4
  • 28
  • 35
Steven
  • 601
  • 8
  • 11
13

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.

dlopez
  • 921
  • 5
  • 16
8

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()
danday74
  • 38,089
  • 29
  • 169
  • 214
  • thanks for this! would be interested to understand why though, if anyone has an explanation i am interested! – Tom Oct 16 '18 at 13:15
  • 1
    I think const store = require('../store/app').default is better ... then just use state as normal - definitely related to use of require, since I've seen this a few times when using require – danday74 Oct 16 '18 at 13:18
  • makes perfect sense indeed. Just to confirm what you said, I have just tried using import and it works. Thanks for your help – Tom Oct 16 '18 at 13:22
6

hope it will help but in my case i got this error because my store was as shown below which is a function:

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

but in index.js i was passing store as a function and not the value it was returning.

wrong

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

correct

<Provider store={store()}>
    <MyApp />
</Provider>
Zahra Shahrouzi
  • 399
  • 3
  • 10
3

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

mcguiretj
  • 61
  • 3