首页 > 解决方案 > 当用户在 React 中按下浏览器后退或下一步按钮时,有什么方法可以提示 MessageBox

问题描述

我写了这样的东西,但我确信它是不正确的。

window.onpopstate = (e) => {
  MessageBox.showConfirmMessage('You clicked back or foward button. Go and lose changes?').then(result => {
    if (!result.confirm) {
      window.history.pushState(null, null, window.location.pathname);
     }
   });
}

标签: reactjs

解决方案


你试过这个(两者)?

window.onbeforeunload = function () {
    return 'Are you sure you want to go leave page?';
};

推荐阅读