目前我正在使用ReactJS构建Web应用程序。该应用程序有一个注册表单。使用setRouteLeaveHook获取错误(withRouter)
现在考虑,用户已经开始注册过程。但在提交表单用户离开此注册页面之前。在这一点上,表单包含未保存的数据,我想显示一条确认消息,说明保存您在离开此屏幕前所做的更改。
下面是我的代码来实现这一
componentDidMount() {
this.props.router.setRouteLeaveHook('/enterprise/enterprise-area/enterprise-details', this.routerWillLeave);
}
routerWillLeave(nextLocation) {
// return false to prevent a transition w/o prompting the user,
// or return a string to allow the user to decide:
if (true) {
return 'Your work is not saved! Are you sure you want to leave?';
}
}
export default withRouter(connect(
mapStateToProps,{
initializeVendorDetails
})(VendorRegistration));
我得到如下所示的错误:
Uncaught TypeError: Cannot assign to read only property '__id__' of /enterprise/enterprise-area/enterprise-details
我通过官方文件和github上的问题去,但一无所获。感谢预期。
,我猜你的组件是不与“”直接相连,如“ ” 如果是这样,则必须通过this.props.rout呃从Route组件到YourComponent。 –