反应历史取代禁止后退按钮

时间:2017-09-12 16:10:48

标签: 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?

1 个答案:

答案 0 :(得分:0)

您可以尝试某种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.';
};