Search code examples
reactjsuse-effect

will useEffect callback clean up run if user closes tab or goes to a different website?


useEffect(() => {

  return () => clean_up();

}, [])

Will clean_up be runned if use closes tab or switches to a different website?


Solution

  • No, and this is not particular to React. Once a document or window is closed (which includes being redirected to another page), no further JavaScript can execute in that window.

    setTimeout will not run after the page is closed or redirected either, and neither will any other APIs provided by any other libraries, if they run on the client-side.