问题描述
我试图以编程方式导航到不同的页面,如此 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。
更多相关信息
之后我尝试使用这种新方法,如此 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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!