DEV Community

Discussion on: Why we use empty array with UseEffect

Collapse
 
brense profile image
Rense Bakker

Bonus points if you also add a cleanup to your useEffect, to cancel the request when the component gets unmounted during fetching. In your current code the state update will produce an error if the component gets unmounted during fetching (cant update state of unmounted component).

Collapse
 
csituma profile image
Clara Situma

what do you think about this dev.to/csituma/do-i-need-to-use-cl...

Collapse
 
brense profile image
Rense Bakker

I mostly agree with that, except that you should also cleanup if you do delayed state updates in your useEffect, to avoid updating state on unmounted components (which will result in an error).

Collapse
 
csituma profile image
Clara Situma

Thank you, that's a great addition