React-router-dom和历史记录不起作用

时间:2020-08-31 19:47:09

标签: javascript reactjs react-router react-router-dom

我正在尝试导入react-router-domhistory以便与React-router一起工作,但是不起作用,并且出现此错误。

enter image description here

我导入了这些软件包:

  • yarn add react-router-dom
  • yarn add history

我正在v14.4.0版的节点下工作。

这是我的代码:

index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import { Router } from 'react-router-dom';
import createHistory from 'history/createBrowserHistory';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';

const history = createHistory();

ReactDOM.render(
  <Router history={history}>
    <App />
  </Router>,
  document.getElementById('root')
);
serviceWorker.unregister();

package.json (用于检查很多xd版本):

{
  "name": "react-router",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "history": "^5.0.0",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-router-dom": "^5.2.0",
    "react-scripts": "3.4.3"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

```

If you need additional information for help me to solve the problem, don't doubt it for ask for.
Thanks a lot!!

1 个答案:

答案 0 :(得分:1)

尝试这样导入:

import { createBrowserHistory } from "history";
const history = createBrowserHistory();