不推荐使用this.props.history(react-router) [英] this.props.history is deprecated (react-router)

查看:1294
本文介绍了不推荐使用this.props.history(react-router)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以编程方式导航到不同的页面,如此 this.props.history.push('/ new-path'); 它有效,但我得到了控制台中的弃用警告说:

I was trying to programatically navigate to a different page like so this.props.history.push('/new-path'); it worked, but I got a deprecation warning in console saying:

警告:[react-router] props.history和context.history已弃用。请在这里使用context.router。更多相关信息 https://github.com/reactjs/react-router/blob/master/upgrade-guides/v2.0.0.md#changes-to-thiscontext

之后我尝试使用这种新方法,如此 this.context.router.push(' / new-path')但这似乎不是正确的方法。

Afterwards I tried to use this new method like so this.context.router.push('/new-path') but this doesn't seem to be the right approach.

推荐答案

也许你忘记将路由器定义为contextType?假设您有一个类似的组件:

Maybe you did forget to define the router as contextType? Assuming you have a component like:

class YourComponent extends React.Component {
      render() {
        return <div></div>;
      }
 }

您必须在您的下方定义contextTypes,如下所示组件:

You have to define the contextTypes as follows right beneath your component:

YourComponent.contextTypes = {
  router: React.PropTypes.object.isRequired
};

这篇关于不推荐使用this.props.history(react-router)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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