Question Details

No question body available.

Tags

javascript reactjs react-hooks

Answers (2)

Accepted Answer Available
Accepted Answer
August 16, 2025 Score: 10 Rep: 221 Quality: High Completeness: 40%

Typically no, the cleanup function in useEffect won’t run on window.location.reload(). Cleanup only runs when React unmounts a component or reruns the effect, but a full page reload wipes out the JS runtime before React can do anything. If you need logic before reload/close, use beforeunload instead.

September 29, 2025 Score: 3 Rep: 31 Quality: Medium Completeness: 80%

When we call window.location.reload():

  • The entire page i.e. React, the DOM, and our JavaScript environment are torn down by the browser.

  • This does not give React a chance to unmount component, instead the browser just throws everything away and reloads from scratch, which means React does not run our cleanup functions.

    Let's take this example :

useEffect(() => {
  console.log("Effect runs in line 1");

return () => { console.log("Cleanup runs in line 15"); }; }, []);

const handleReload = () => { window.location.reload(); };

When the component first mounts, we'll see 'Effect runs in line 1', now when we click a button that triggers reload, the browser discards the page. So we'll never see 'Cleanup run in line 15', cause React never got a chance to execute it.

Coming to the point answer : No window.location.reload() does not trigger the useEffect cleanup.
The browser reload wipes the page, React is gone, so React can’t run the cleanup.