Custom hook that returns a safe version of setTimeout which will clear all timeouts when the component unmounts.
This hook ensures that any timeouts set within the component are cleared when the component unmounts,
avoiding potential memory leaks.
Parameters
Optionaldependencies: DependencyList = []
List of dependencies for the effect that clears timeouts.
Returns (callback:()=>void,delay:number)=>Timeout
A safe version of setTimeout.
Example
constsafeSetTimeout = useSetTimeout();
useEffect(() => { consttimeoutId = safeSetTimeout(() => { console.log('This will run after 1 second'); }, 1000); return () => { clearTimeout(timeoutId); // Optional, since useSetTimeout will clear it on unmount }; }, [safeSetTimeout]);
Custom hook that returns a safe version of setTimeout which will clear all timeouts when the component unmounts.
This hook ensures that any timeouts set within the component are cleared when the component unmounts, avoiding potential memory leaks.