How to cancel all subscriptions inside UseEffect in React

dlya :

I am getting this error - Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.

Here's my useEffect hook, I used a ref called mounted to check if the component has unmounted or not but I am still getting the error when the component unmounts. (It takes about a minute for the error to show up).

    useEffect(() => {
            if(mounted.current){
                if(mincounter === 0 && hrcounter > 0){ 
                    setHrcounter(hrcounter - 1);
                    setMincounter(60);
                    mincounter > 0 && setTimeout(() => setMincounter(mincounter - 1) , 1000*60)
                }else if (mincounter === 0 && hrcounter === 0){
                    submitHandler()
                }else{
                    mincounter > 0 && setTimeout(() => setMincounter(mincounter - 1) , 1000*60)
                }
            }
        return () => {
            mounted.current = false
            console.log('info tab unmounting', mounted.current);
        }
    }, [mincounter, hrcounter, submitHandler,setHrcounter,setMincounter]);

TIA

Victor :
   const [subscriptions, setSubscriptions] = useState([]);

I usually store all my subscriptions on my component state and then call them when component will be un mounted (in the cleanup of useEffect hook)

Like this:

   useEffect(() => {
      const subscription1 = ...
      const subscription2 = ...
      // When you create subscriptions, just store them on state
      setSubscriptions([...subscriptions, subscription1, subscription2]);

      // Cleanup (Detach subscriptions)
      return () => subscriptions.forEach(subscription => subscription)

   },[])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

React - To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function

How to cancel all subscriptions and asynchronous tasks in a useEffect cleanup function?

How to Resolve Memory Leak and Cancel All Subscriptions Error in React App?

How to cancel cancel all subscriptions and asynchronous tasks?

Cancel All Subscriptions and Asyncs in the componentWillUnmount Method, how?

how can I cancel subscriptions in react

cancel all subscriptions in componentWillUnmount

"To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function." error But I'm not using it

state update on an unmounted component. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function

cancel all subscriptions and asynchronous tasks

how to cancel all ajax request inside for loop

Cancel all subscriptions and asynchronous tasks in the componentWillUnmount method

Streaming of DolphinDB: Cancel all subscriptions on the current node

how to cancel timeout when next calling arrives in useEffect react

How to use useEffect inside a View in React Native

How to stop the infinite loop inside this React useEffect?

React Hooks: How to setState inside useEffect?

How to call an async function inside a UseEffect() in React?

Stripe subscriptions - how to format cancel_at date

Promise.all() inside useEffect in react returns an array of undefined items

React setInterval inside useEffect

react - function inside useEffect

What is the right way to cancel all async/await tasks within an useEffect hook to prevent memory leaks in react?

Can't perform a React state update on an unmounted component. Cancel all tasks in a useEffect

I keeping getting this error "cancel all subscriptions and asynchronous tasks in the componentWillUnmount"

Unable to cleanup useEffect, how can I cancel all asynchronous tasks when unmounting my component?

How to simplify an async call inside a React useEffect Hook

How to trigger an event after n seconds inside a react useEffect hook

How do I chain async methods inside React useEffect() hook