Fetching API data in useEffect is always tricky and you should always remember to cancel you subscription here. Your component may be unmounted when promise resolves and this will try to set state that will cause memory leaks. You will also see warning in console about this.
Fetching API data in
useEffect
is always tricky and you should always remember to cancel you subscription here. Your component may be unmounted when promise resolves and this will try to set state that will cause memory leaks. You will also see warning in console about this.Or we can use a bit of magic :) (Live Demo)
I definitely have to check Axios out!
I didn't know about that, so thank you very much for pointing that out! I'll take this into account in future implementations of
useEffect
.