[英]Navigate to the URL without react-router - “_this.props.history is undefined” error
[英]this.props.history is deprecated (react-router)
我试图以编程方式导航到不同的页面,如this.props.history.push('/new-path');
它工作,但我在控制台中得到了一个弃用警告说:
Warning: [react-router] props.history and context.history are deprecated. Please use 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')
但这似乎不是正确的方法。
也许你忘记将路由器定义为contextType? 假设您有一个组件,如:
class YourComponent extends React.Component {
render() {
return <div></div>;
}
}
您必须在组件下方定义contextTypes,如下所示:
YourComponent.contextTypes = {
router: React.PropTypes.object.isRequired
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.