DEV Community

Cover image for Avoiding race conditions and memory leaks in React useEffect

Avoiding race conditions and memory leaks in React useEffect

saransh kataria on February 10, 2021

Let us learn how to deal with the “Can’t perform a React state update on an unmounted component” warning Let us take a look at an implementation o...
Collapse
 
gpaoloni profile image
Gianfranco Paoloni

Great post, didn't knew about the AbortController, just learned cool stuff, thanks!

Just one small correction, I think that in the cleanup function of the AbortController example, you want to call abortController.abort().

Collapse
 
havespacesuit profile image
Eric Sundquist

Additionally, you can check the status of the abort signal before doing any state-changing assignments.

if (!abortController.signal.aborted) { /* set state */}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
saranshk profile image
saransh kataria

Though that can be done, it will never return true since whenever the signal will be aborted, the code would enter the catch block because of the exception thrown.

Collapse
 
saranshk profile image
saransh kataria

Thanks, that was a typo and I have updated it.

Collapse
 
paras594 profile image
Paras 🧙‍♂️

yes, I was thinking the same.

Collapse
 
thebuildguy profile image
Tulsi Prasad

Great post man, that way you declared a variable inside useEffect to know when the component is mounted or not, was genius 💥

Collapse
 
saranshk profile image
saransh kataria

Thank you. Glad it was helpful! Yeah the variable to know if the component is mounted or not was something that I had come across when doing a related task and it was super helpful to do it that way.

Collapse
 
zackdotcomputer profile image
Zack Sheppard

One of the few advantages that axios has over fetch is that it has Cancel support including on IE, so if you have to support IE it might be good to use a library to polyfill in cancellation?

Collapse
 
saranshk profile image
saransh kataria • Edited

I would rather use a polyfill than use axios, but that is again a personal preference. If there are other features that axios provides (specific to the use case), I might go for it. You could just use the first approach of a boolean flag as well.

But if it is just cancellation support for IE, you could take a look at the example repo

Install:

  • promise-polyfill
  • unfetch
  • abortcontroller-polyfill

Add the following:

import 'promise-polyfill/src/polyfill';
import 'unfetch/polyfill';
import 'abortcontroller-polyfill';
Enter fullscreen mode Exit fullscreen mode
Collapse
 
devhammed profile image
Hammed Oyedele

There are other features like request and response interceptors that axios provides by the way.

Thread Thread
 
saranshk profile image
saransh kataria

As I mentioned, if there are other features that you are using, go for it. But most people are not using those features, thus the recommendation.

Collapse
 
paras594 profile image
Paras 🧙‍♂️

This is cool. I use axios and its cancel token. This was helpful !! :)

Collapse
 
saranshk profile image
saransh kataria

Thank you! Glad it was helpful. I prefer libraries only when there is a compelling need to use them. :)

Collapse
 
yougotwill profile image
Will G

Nice post! Was clear and easy to understand.

Collapse
 
saranshk profile image
saransh kataria

Thank you!

Collapse
 
wdavidcalsin profile image
WDavid Calsin

I liked the post, I learned a lot about AbortController, it seems interesting as I had never heard about it, thanks.

Collapse
 
saranshk profile image
saransh kataria

Glad it helped. Thank you!

Collapse
 
saranshk profile image
saransh kataria

Thank you! fetch has become my go-to recently over any libraries.