Redux Router Uncaught TypeError:createHistory不是函数

时间:2016-12-01 10:46:04

标签: reactjs history redux-router

Redux路由器版本^ 2.1.2

react-router version:"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

错误变为{{1}}

思想?

1 个答案:

答案 0 :(得分:5)

我通过在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还没有与历史v4兼容。我不确定为什么npm没有观察到react-router v3.0.0 package.json中的依赖性