【问题标题】:browserHistory.push not working in reactbrowserHistory.push 在反应中不起作用
【发布时间】:2018-01-03 19:45:33
【问题描述】:

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

这里的问题是 url 正在更改,但组件未呈现。 无法理解。

browserHistory.push("/personalInfo"); 

这就是我尝试更改标签的方式。 网址将更改为 http://localhost:3000/personalInfo。但是组件没有渲染,当我刷新浏览器时,它正在改变。

【问题讨论】:

  • 你为什么不使用相同的链接?链接就像锚标签。 knowbody.github.io/react-router-docs/api/Link.html
  • 正在使用哪个版本的react-routerbrowserHistory 在最新的 react-router (v.4) 中被删除,所以我猜它是一个旧版本。您没有使用最新版本是您项目的限制吗?

标签: reactjs react-router


【解决方案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);

【讨论】:

    猜你喜欢
    • 2019-02-21
    • 2017-08-31
    • 1970-01-01
    • 2016-05-13
    • 2021-03-08
    • 2021-01-29
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多