【问题标题】:Redux Router Uncaught TypeError: createHistory is not a functionRedux Router Uncaught TypeError: createHistory is not a function
【发布时间】:2016-12-01 10:46:04
【问题描述】:

Redux 路由器版本 ^2.1.2

react-router 版本:"react-router": "^3.0.0", 历史版本:"history": "^4.3.0",

重现步骤

    import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware, compose } from 'redux';
import { Router, browserHistory } from 'react-router';
import { reduxReactRouter, routerStateReducer, ReduxRouter } from 'redux-router';
import reduxThunk from 'redux-thunk';
import { createHistory } from 'history';

import rootReducer from './core/rootReducer';
import routes from './core/routes';

import * as axios from './utils/axios.js';

import { AUTH_USER } from './authentication/types';

const store = compose(applyMiddleware(reduxThunk),
    reduxReactRouter({ routes, createHistory })
)(createStore)(rootReducer);

const token = localStorage.getItem('token');

if (token) {
    store.dispatch({ type: AUTH_USER });
}

ReactDOM.render(
    <Provider store={store}>
        <ReduxRouter>

            {routes}

        </ReduxRouter>
    </Provider>
    , document.querySelector('.main')
);

返回错误index.js:34416 Uncaught TypeError: createHistory is not a function(…)

有什么办法吗?


import { createHistory } from 'history';改为import createHistory from 'history/createBrowserHistory';

错误变成Uncaught TypeError: history.getCurrentLocation is not a function

想法?

【问题讨论】:

  • 您找到解决方案了吗?我不知道为什么它被否决了,我遇到了完全相同的问题。

标签: reactjs history redux-router


【解决方案1】:

我通过在 package.json 中指定历史版本 3 来解决此问题

"history": "^3.0.0",

react-router 3.0.0 将 history 3.0.0 指定为依赖项,但由于某种原因,它没有添加到 node_modules 中。但是将它显式添加到您自己的 package.json 为我解决了这个问题。

我不认为 react-router 3.0.0 与 history v4 兼容。我不确定为什么 npm 没有观察到 react-router v3.0.0 package.json 中的依赖关系

【讨论】:

猜你喜欢
  • 2018-12-31
  • 1970-01-01
  • 1970-01-01
  • 2020-07-11
  • 1970-01-01
  • 2014-08-15
  • 2021-12-16
  • 2018-04-20
  • 2017-04-13
相关资源
最近更新 更多