javascript - react 历史记录替换不允许后退按钮

标签 javascript reactjs react-router

我知道从技术上讲不可能禁止用户单击浏览器的后退按钮,但有时在我们的应用程序中允许这种操作是没有意义的。

例如,在我的工作申请应用程序中,申请后您会转到下一页,

this.props.applyJob({
      id: this.state.selectedAd_Id,
      applicant_id
    }).then(response => {
      if(response.value.status === 200){
        const { id } = response.value.data.result
        this.props.history.replace(`/job/applied/${id}`)
      }
    })

but user still can go to the previous page which doesn't make sense, how to handle that?

最佳答案

您可以尝试某种 onunload 函数。如果用户单击您希望他们单击​​的按钮,您必须设置一个变量,这样他们就不会收到警告消息。

    var showAlert= 1; // this is set to it will show warning when leaving the page

    // if they click your next button add this to a function
    // showAlert =0;

window.onbeforeunload = function(){
    if (showAlert == 1)
  return 'Please use the Next and Previous buttons and not the browser back buttons to ensure your date is saved.';
};

关于javascript - react 历史记录替换不允许后退按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46181140/

相关文章:

javascript - 不要继承我的 Chrome 扩展中网页的 CSS

javascript - runInNewContext 究竟做了什么?

javascript - react : 'Redirect' is not exported from 'react-router-dom'

javascript - Github 页面 HashRouter 不显示组件

javascript - 通过 slickgrid 中的 seacrh 字段将滚动条移动到特定行

Java Play 框架 Javascript 中的 Groovy 脚本

javascript - 状态正在更新但未显示

swift - 整合 Vapor 和 React

javascript - 使用 Javascript 或 Typescript 编写 React.js 应用程序

reactjs - 单击子组件时隐藏父 react 组件