this.props.history is deprecated (react-router)

匿名 (未验证) 提交于 2019-12-03 03:08:02

问题:

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:

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


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.

回答1:

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>;       }  } 

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

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!