在React Router DOM的历史记录导入中未定义createBrowserHistory

时间:2019-11-07 16:23:01

标签: reactjs react-router react-router-dom history.js

我目前正在使用React Router DOM的v5。我的目标是创建一个历史记录对象,以便可以在我的redux操作中操纵页面位置。在过去的项目中,我是这样导入createBrowserHistory函数的:

import createBrowserHistory from 'history/createBrowserHistory'

export default createBrowserHistory();

使用此设置,尽管我收到了弃用警告:

Warning: Please use `require("history").createBrowserHistory` instead of `require("history/createBrowserHistory")`. Support for the latter will be removed in the next major release.

我将导入更改为:

import createBrowserHistory from 'history';

但此时createBrowserHistory出现为undefined。我现在不知所措。任何帮助将不胜感激。下面是我的yarn.lock文件的样子:

react-router-dom@^5.1.2:
  version "5.1.2"
  resolved "https://registry.yarnpkg.com/react-router-dom/-/react-router-dom-5.1.2.tgz#06701b834352f44d37fbb6311f870f84c76b9c18"
  integrity sha512-7BPHAaIwWpZS074UKaw1FjVdZBSVWEk8IuDXdB+OkLb8vd/WRQIpA4ag9WQk61aEfQs47wHyjWUoUGGZxpQXew==
  dependencies:
    "@babel/runtime" "^7.1.2"
    history "^4.9.0"
    loose-envify "^1.3.1"
    prop-types "^15.6.2"
    react-router "5.1.2"
    tiny-invariant "^1.0.2"
    tiny-warning "^1.0.0"

1 个答案:

答案 0 :(得分:0)

尝试

import history from 'history';
var createBrowserHistory = history.createBrowserHistory 

import { createBrowserHistory } from 'history';