简体   繁体   English

不推荐使用this.props.history(react-router)

[英]this.props.history is deprecated (react-router)

I was trying to programatically navigate to a different page like so this.props.history.push('/new-path'); 我试图以编程方式导航到不同的页面,如this.props.history.push('/new-path'); it worked, but I got a deprecation warning in console saying: 它工作,但我在控制台中得到了一个弃用警告说:

Warning: [react-router] props.history and context.history are deprecated. Please use context.router. more about it here https://github.com/reactjs/react-router/blob/master/upgrade-guides/v2.0.0.md#changes-to-thiscontext 更多关于它https://github.com/reactjs/react-router/blob/master/upgrade-guides/v2.0.0.md#changes-to-thiscontext


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. 之后我尝试使用这个新方法,如this.context.router.push('/new-path')但这似乎不是正确的方法。

Maybe you did forget to define the router as contextType? 也许你忘记将路由器定义为contextType? Assuming you have a component like: 假设您有一个组件,如:

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

You have to define the contextTypes as follows right beneath your component: 您必须在组件下方定义contextTypes,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM