1.需求:在离开页面时,提示用户当前页面的数据尚未保存,是否离开
2.方案:react-router 4中自带此类组件Prompt
官方文档 参考文档 自定义组件
3.参数:
when:设置是否启用Prompt功能。比如表单页未填写时,就不需要离开确认。 message:string。设置Prompt提示内容 message:function。此时返回参数分为true/false,即bool类型。返回false就继续停留在当前页面,返回true则跳转到新页面。
4.我的简单实现:
<Prompt
when
={isPrompt
}
message
={(location
) => {
let isstop
= location
.pathname
.startsWith('/curriculums/edit/');
if (isstop
|| readonly
) {
return true;
} else {
return '当前页面有未保存或未提交的内容,离开后将丢失已编辑内容,您确定要离开吗?';
}
}}
/>
转载请注明原文地址:https://tech.qufami.com/read-25796.html