question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

client.unsubscribe() not working in useEffect cleanup

See original GitHub issue

I am using useEffect to get a new refresh token and access token when any component is mounted, in App.js, but on each unmount I get the useEffect error “clean async tasks and subscriptions in your useEffect cleanup function” , so I have added this return ()=>client.unsubscribe(), which does not solve the problem and throws an error. How can I solve this ? Thank you !

The useEffect in question:

 let unMounted=useRef(false)

    React.useEffect(() => {

        //We refresh the access token every time the page is changed
        fetch('http://localhost:8000/refresh_token', {
            method: 'POST',
            credentials: 'include',
        }).then(async x => {
            const { accessToken } = await x.json()

            setAccessToken(accessToken)
            setLoading(false)

            
        })

        return ()=>{
            unMounted.current=true
            client.unsubscribe()
        }
    }, [])

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
dylanwulfcommented, Oct 28, 2021

@vpaul18 I think your problem is actually that you’re trying to set state after the component is unmounted. Maybe try something like this?

 let unMounted=useRef(false)

    React.useEffect(() => {

        //We refresh the access token every time the page is changed
        fetch('http://localhost:8000/refresh_token', {
            method: 'POST',
            credentials: 'include',
        }).then(async x => {
            const { accessToken } = await x.json()

+          if (!unMounted.current) {
                setAccessToken(accessToken)
                setLoading(false)
+          }

            
        })

        return ()=>{
            unMounted.current=true
            client.unsubscribe()
        }
    }, [])
1reaction
brainkimcommented, Oct 28, 2021

Thanks @dylanwulf!

Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - useEffect Err: To fix, cancel all subscriptions and ...
useEffect Err: To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function. Firebase · Subscribe to RSS.
Read more >
Problem in unsubscribe of useSubscription react hook #7964
I fixed the cleanup problem by calling subscribeToMore in useEffect and then using its return value as the cleanup function.
Read more >
Cleaning up Async Functions in React's useEffect Hook ...
The instruction is pretty clear and straightforward, "cancel all subscriptions and asynchronous tasks in a useEffect cleanup function". Alright, ...
Read more >
Demystifying useEffect's clean-up function - Max Rozen
Thankfully, not knowing how useEffect's clean-up function works isn't as bad as getting the dependency array wrong, or passing constantly redeclared functions ...
Read more >
My React app is opening multiple connections - Ably FAQs
... classical components, and not unsubscribing in componentWillUnmount(). ... returned a cleanup function that unsubscribes during a call to useEffect ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found