browserHistory.push 在react中不起作用

IT技术 reactjs react-router
2021-05-22 08:49:49

我对 reactJs 很陌生。我正在尝试通过单击按钮更改选项卡。为此,我正在使用browserHistory.

这里的问题url是正在改变,但组件没有被渲染。无法理解这一点。

browserHistory.push("/personalInfo"); 

这就是我尝试更改选项卡的方式。网址正在更改为http://localhost:3000/personalInfo. 但是组件没有呈现,当我刷新浏览器时,它会发生变化。

1个回答

利用 withRouter

import React, { Component } from "react";
import {withRouter} from "react-router-dom";

class MyComponent extends Component {

  sampleFunction() {
    this.props.history.push("/personalInfo");
  }

}
export default withRouter(MyComponent);