Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

useEffect cleaning function is not called when tab is closed

I have this useEffect which I use just for cleaning:

  useEffect(() => {
    return function cleanup() {
      if (!room || !currentPortal) return;
      leavePortal(
        room,
        currentPortal,
        currentUserProfile && currentUserProfile.uid
          ? currentUserProfile.uid
          : uniqueId
      );
      detachListener();
    };
  }, [isFirstLoad, currentUserProfile, currentPortal]);

I can go back and forth and it works just fine, but does nothing if the tab is closed. Is that how useEffect works? Does it not detect the tab closing?

like image 272
Tsabary Avatar asked Apr 19 '20 18:04

Tsabary


People also ask

Why is useEffect cleanup called?

What is the useEffect cleanup function? Just like the name implies, the useEffect cleanup is a function in the useEffect Hook that allows us to tidy up our code before our component unmounts. When our code runs and reruns for every render, useEffect also cleans up after itself using the cleanup function.

How do I useEffect with cleanup function?

The hook comes with a cleanup function, which you might not always need, but it can come in handy. To invoke the cleanup function you can simply add a return function like so: useEffect(() => { // Your effect return () => { // Cleanup }; }, []); The cleanup can prevent memory leaks and remove unwanted things.

When exactly does react clean up an effect?

React performs the cleanup when the component unmounts. However, as we learned earlier, effects run for every render and not just once. This is why React also cleans up effects from the previous render before running the effects next time.

Does useEffect get called on unmount?

This is because once we put count in the dependency array, useEffect is only called on mount, unmount, and when count changes between renders.

What is the purpose of the cleanup function on the useeffect?

This is useful besides the cleanup function to prevent memory leaking. There's a lot of other usages of the cleanup function on the useEffect hook, but I hope this can give you a better perspective of how and when to use it. Please add any comment or suggestion, I will appreciate.

What is cleaning up effect in JavaScript?

Once the effects are created, then they are needed to be cleaned up before the component gets removed from the DOM. For this, cleaning up effect is used to remove the previous useEffect hook’s effect before using this hook of the same component again.

When is the clean up function called on unmount?

The cleanup function returned from an effect is only ever explicitly called on unmount if you pass an empty array as the second argument. Otherwise, the clean up function will be called whenever something in the dependency array changes. A local variable can be used inside the useEffect callback. Credit to @gaearon

Can a user leave a screen before a process ends?

In a real scenario, it happened to me on React Native that an user can leave a screen before a process ends. In the following example, we have an async function that performs some operation and while this is running I want render a “loading” message. Once the function finish I will change the state of “loading” and render another message.


Video Answer


1 Answers

useEffect will not detect tab close by default.

However you can implement that by yourself:

useEffect(() => {
  const cleanup = () => {
    // do your cleanup
  }

  window.addEventListener('beforeunload', cleanup);

  return () => {
    window.removeEventListener('beforeunload', cleanup);
  }
}, []);
like image 99
rksh1997 Avatar answered Sep 30 '22 02:09

rksh1997