supasate / connected-react-router

A Redux binding for React Router v4
MIT License
4.73k stars 594 forks source link

TypeError: Cannot read property 'location' of undefined with Redux-persist #294

Closed gyto closed 5 years ago

gyto commented 5 years ago

I am trying to get React app to work properly with React Router and Redux, but I constantly getting TypeError: Cannot read property 'location' of undefined to any components that have redux

store/configureStore.js

import { createStore, applyMiddleware, compose } from 'redux';
import reducer from '../reducers';
import { persistStore } from 'redux-persist';
import { createBrowserHistory } from 'history';
import { routerMiddleware  } from "connected-react-router";

let _persistor;
let _store;

export const history = createBrowserHistory();

export const getStore = (props) => {
    if (_store) {
        return _store;
    }
    const initialState = (props) ? {...props} : {};
    _store = createStore(
        reducer,
        initialState,
        compose(
            applyMiddleware(routerMiddleware(history)),
            window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
        ),
    );

    _persistor = persistStore(_store);

    return _store;
};

export const getPersistor = () => {
    return _persistor;
};

reducers/index.js

import { combineReducers } from 'redux';
import { persistReducer } from 'redux-persist';
import Menu from './Menu';
import SelectedServices from './SelectedServices';
import { connectRouter } from 'connected-react-router'
import { history } from "../store/configureStore";

const rootReducer = (history) => combineReducers({
    router: connectRouter(history),
    menu: Menu,
    selectedServices: SelectedServices,
});

const reducer = persistReducer(
    {
        key: 'root',
        storage: require('localforage'),
        whitelist: [],
    },
    rootReducer(history),
);

export default reducer;
gyto commented 5 years ago

I have posted same question on stack overflow its here