0

I'm trying to detect browser reload in my react application. I've looked around many solutions but no solution was right for me.

What I want to do is detect browser reload from a specific page. Some of the solutions I tried were almost correct but the issue is it cannot differentiate the browser reload and normal navigation. For ex: the solutions I used could trigger when the browser is being reloaded. But they also trigger when I come to that page using router. What can I do to limit my solution to only trigger when the browser is being reloaded.

The solutions I tried

  useEffect(()=>{
    if (typeof window !== undefined) {
      if (window.performance) {
        if (performance.getEntriesByType("navigation")[0].type === 'reload') {
          alert('This page is reloaded');
        } else {
          alert('This page is not reloaded');
        }
      }
    }
  },[])

And

  useEffect(() => {
    console.log("AAAAAAA",window.performance.navigation)
  },[]);
DᴀʀᴛʜVᴀᴅᴇʀ
  • 5,839
  • 15
  • 57
  • 101
CraZyDroiD
  • 5,958
  • 28
  • 74
  • 160
  • https://stackoverflow.com/questions/50026028/react-how-to-detect-page-refresh-f5 same question. Should find the answer there. – aktoriukas Apr 19 '22 at 15:30

0 Answers0