client.unsubscribe() not working in useEffect cleanup
See original GitHub issueI 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:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@vpaul18 I think your problem is actually that you’re trying to set state after the component is unmounted. Maybe try something like this?
Thanks @dylanwulf!