在反应路由器documentation为onEnter
钩他们指定如何处理,但onLeave
没有例子。我的问题是如何确认用户想要正确离开页面。在反应路由器是onEnter是onLeave
根据以下两种情况给出的代码,无论用户点击cancel
还是ok
,它都会离开页面。但OnEnter钩按逻辑工作正常。
const checkEnterAbout = (nextState, replace, callback) => {
if(!confirm('do you want to enter really!!!')){
replace(`/`);
}else{
callback();
}
}
const checkLeaveAbout = (prevState) => {
console.log(prevState);
return confirm("Are you sure you want to leave this page");
}
<Route path="/about" component={About} onEnter={checkEnterAbout} onLeave={checkLeaveAbout} />
另外当我试着withRouter确认指南。它给我的
Failed prop type: Invalid prop `component` supplied to `Route`.
in Route
您是否想在** **的任何页面的用户离开的确认?或者只是关于页面? – samvv
只是关于页面 – owaishanif786