I have a popup and I want that popup to be enabled for 3s. So I used setTimeout for 3s. Now I want to clear the timeout if the screeen is unmounted. But using this is giving me warning on lighthouse that unload is deprecated.
Any alternative of cleanup function ?
useEffect(() => {
return () => {
clearTimeout(popupTimeoutId);
};
}, []);
I am using cleanup function but needs a solution that can help me remove the lighthouse warning.
There is no React
useEffecthook cleanup function alternative. The Lighthouse deprecation/warning has nothing to do with theuseEffecthook though. It would seem that somewhere in your app you have instantiated awindow.unloadevent listener, which is a deprecated API.There was also a deprecation warning from Chrome: Deprecating the unload event.
I would suggest switching to the beforeunload event, though the above Chrome page suggests also trying/using visibilitychange and pagehide events as Alternatives to unload events.
At the end of the day though, this is still just a warning, and you are free to ignore it with the understanding that your app/page may not function properly or as you expect in some browsers.