DEV Community

Discussion on: 💡 React Hooks: async function in the useEffect

Collapse
 
danialdezfouli profile image
Danial Dezfouli

As Samuel mentioned, we can check if the component is unmounted or not to update the state.

Thread Thread
 
icyjoseph profile image
Joseph

Not really... You shouldn't really be doing handcrafted isMounted checks at all. To prevent state updates from an unstoppable promise/async function, use a ref on an HTML element on whatever you are rendering on this component, if the ref.current value is null then throw in the promise or exit the async function gracefully, skipping any updates.

Of course for fetch/axios there's a proper abort/cancel mechanism.