Redux-Persist和Connected-React-Router:TypeError:无法读取未定义的属性“位置”

时间:2019-04-16 13:48:17

标签: reactjs redux redux-persist connected-react-router

我试图使React应用程序与React Router和Redux一起正常工作,但是我不断向具有Redux的任何组件获取TypeError: Cannot read property 'location' of undefined。我正在使用connected-react-router lib在商店中注册路由器,但显然我做错了事

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;

1 个答案:

答案 0 :(得分:0)

经过三个小时的调试,并详细了解了connected-react-routerreact-reduxreact-router-dom的每个库,我发现其中一行跳过了几次,但最终我将其检查了一下package.json。目前:

  

v6.0.0需要React v16.4.0和React Redux v6.0。 (它尚不支持React Redux v7.0。我们正在研究兼容性问题。)

我正在使用react-redux: v7.0.1,它对我来说是一个真正的脸红。所以我降级到v6,一切正常。