2

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

存储/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;
};

减速器/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;
4

1 回答 1

1

经过三个小时的调试和connected-react-router阅读react-redux更多react-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,一切开始正常。

于 2019-04-16T21:04:45.193 回答