-
Notifications
You must be signed in to change notification settings - Fork 47.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature proposal: Give useEffect
cleanup an option to run on page unload
#32369
Comments
useEffect
cleanup an option to run on page unloaduseEffect
cleanup an option to run on page unload
Should that be on the actual page There's so--- many variants at play here, that this is just a bad idea. Imho, if you want this type of behavior you should really just create a custom |
Hi @rjgotten, Thank you for your insights on this proposal. I’m curious about how the proposed useEffect cleanup behavior on page unload might interact with Single Page Application (SPA) navigations or redirects within a React app. Specifically, could it help address issues such as lingering styles from the previous route when navigating between pages in a client-side rendered application? Would you recommend handling such cleanup with the proposed useEffect flag, or do you think a custom hook (e.g., usePageHideEffect) would be more suitable for this type of scenario? Thanks and best wishes, |
I had the same problem and I’ve been using the following workaround: export const runOnPageHide = (cleanUp: () => void) => {
const newCleanUp = () => {
cleanUp();
window.removeEventListener("pagehide", newCleanUp);
};
window.addEventListener("pagehide", newCleanUp);
return newCleanUp;
}; Then to use it you just have wrap the clean up function of any regular effect with useEffect(() => {
// do stuff
return runOnPageHide(() => {
// do clean up
}),
}, [some, dependencies]) Seems to work fine. Note that in my case, the React app runs in a popup window where I don’t have to worry about the BF-cache or about |
I would either build an application-specific custom hook, or would go for the full bespoke solution that @guillaumebrunerie brings up, which is essentially the same thing - but hidden behind less abstraction. Adding an additional flag parameter to Also; there's the whole thing where flag parameters that alter implementation specifics can be considered a code smell. For good reason, because it leads to the anti-pattern of "configuration-over-composition" where over time you expand one particular unit of code with additional configuration again and again, bloating it and violating the single-responsibility principle. Case in point: if you'd say: "Well, okay-- so a simple boolean toggle isn't enough. Let's pass an options object instead," then you're already headed for this territory because you'll have to specify the type of DOM event. As You'd also still need a way to set apart the page-unload cleanup logic from the regular component cleanup logic that's run when the effect dependencies change or the component unmounts. And where multiple different types of And this doesn't even address the elephant in the room of setting aside real page navigations that cause actual document (re)loads from a server, with virtual In short:
That's more of a problem with the |
Currently cleanup functions returned by
useEffect
hook are not executed on page unload, and for a good reason - most cleanups do not need to be called when the page is closing. But there are some situations, when it is useful. To name but a few:close()
method onWebSocket
orEventSource
instances;keepalive = true
.Currently, React does not have a built-in solution for this. It is possible to write something like:
but this solution does not have an advantage of statically checked dependencies. The alternative would be:
but there is no guarantee that the caller will not forget to use
useCallback
My proposal is to add an optional flag to the hook:
that would make cleanup run on page onload.
The text was updated successfully, but these errors were encountered: