browserHistory.push 在反应中不起作用 [英] browserHistory.push not working in react

查看:40
本文介绍了browserHistory.push 在反应中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am very new to reactJs. I am trying to change the tab on click of button. For this I am using browserHistory.

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

Problem here is url is getting changed but component is not getting rendered. Couldn't understand this.

browserHistory.push("/personalInfo"); 

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

This is how I am trying to change the tab. Url is getting changed to http://localhost:3000/personalInfo. But component is not rendered, when I refresh the browser, it's getting changed.

推荐答案

Use 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);

这篇关于browserHistory.push 在反应中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆