React报错 :browserHistory doesn't exist in react-router

由于版本问题,React中history不可用

import { hashHistory } from 'react-router'

首先应该导入react-router-dom包:

import { hashHistory } from 'react-router-dom'

以前的写法:

import React from 'react';
import { hashHistorty } from "react-router";

class Login extends React.Component {
  ...
  onSubmit() {
... hashHistory.push('/GetUser'); } ... }

这种方式会报错:

'react-router' does not contain an export named 'hashHistory'.

可以用 history包 (需要安装 npm install --save history )进行修改:

import React from 'react';
import { createHashHistory } from 'history';

const history = createHashHistory();

class Login extends React.Component {
  ...
  onSubmit() {
    ...
     history.push('/GetUser');        
  }       
  ...
}

参考:https://stackoverflow.com/questions/36467126/reactjs-can-not-read-property-push-of-undefined/43266270

(毕)