javascript - 单击确定时如何向 React Router 的提示添加功能?
问题描述
我正在做一个多步骤表单,我想防止用户在此过程中离开。这个想法是,如果用户在执行该过程时尝试离开,则向他们显示确认消息,如果他们确认,则将他们重定向到主页,否则留在当前页面。
目前我正在使用来自 React 路由器的 < Prompt/>
<Prompt when={isLeaving} message={"Changes will be lost"}/>
这会显示确认信息,因此如果用户接受,我想将用户重定向到主页。
我也尝试了事件监听器,我几乎完成了它。唯一的问题是它只检测popstates。
useEffect(() => {
if(!popEventListnerAdded){
window.addEventListener('popstate', handleBackButton, false);
popEventListnerAdded = true;
}
}, []);
const handleBackButton = event => {
var r = window.confirm("You will need to begin the process again are you sure?");
if (r === true) {
// Call Back button programmatically as per user confirmation.
history.push("/Home");
} else {
// Stay on the current page.
history.push(location.pathname)
}
}
当用户在 < Prompt /> 上单击“确定”时,有什么方法可以添加功能以便我可以重定向它们?
非常感谢!
解决方案
推荐阅读
- javascript - Javascript - 如何拆分多维数组数据并利用它
- c# - CultureInfo.CurrentCulture 错误
- macos - 如何在 MACOS 中使用 swift 将具有 PDFActionGoto 操作类型的链接注释添加到 PDFPage
- vb.net - 如何在 vb.net 中循环 string()() 数组以附加字符串?
- c# - 在 C# 中传递具有不同类型的泛型参数列表
- javascript - 页面加载后运行php函数
- r - 如何使用大型数据集的 p 值实现层次聚类?
- html - 输入字段不响应弹性框
- laravel - 控制器传递的视图变量中不能使用多个 foreach
- c++ - C++ 线程为什么即使在 fflush 之后也没有打印 cout 语句