Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

will calling window.location.reload() trigger the clean up function inside a useEffect?

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

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

If window.location.reload() is called anywhere in your application, will the cleanup callback of the useEffect fire?

like image 363
Big Cat Public Safety Act Avatar asked Oct 23 '25 15:10

Big Cat Public Safety Act


1 Answers

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.

like image 94
RomboCombo Avatar answered Oct 25 '25 04:10

RomboCombo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!