React | How to detect Page Refresh (F5)

后端 未结 4 1406
北海茫月
北海茫月 2020-12-03 10:57

I\'m using React js. I need to detect page refresh. When user hits refresh icon or press F5, I need to find out the event.

I tried with stackoverflow po

4条回答
  •  星月不相逢
    2020-12-03 11:26

    Place this in the constructor:

    if (window.performance) {
      if (performance.navigation.type == 1) {
        alert( "This page is reloaded" );
      } else {
        alert( "This page is not reloaded");
      }
    }
    

    It will work, please see this example on stackblitz.

提交回复
热议问题