应用程序.js
这是事件处理按钮单击:
this.handleClick = this.handleClick.bind(this);
handleClick(e) {
debugger;
e.preventDefault();
this.context.router.history.push('/HomePage');
}
<button onClick={this.handleClick}>Navigate </button>
TypeError:无法读取未定义的属性“历史”
我安装了这个使用: npm install prop-types --save
它不工作。
原文由 Allen Bert 发布,翻译遵循 CC BY-SA 4.0 许可协议
要验证为什么不工作,你可以
console.log()
你的道具,你会看到history
不会出现,那么你怎么能解决这个问题?好吧,这很容易,您有两种方法:withRouter
HOC例子:
<Redirect />
组件例子:
我希望这对你们有所帮助!