Unmounts & useState in async callbacks

If you need to call an api and fetch data when component mounts then use useEffect

If you want to call it on onClick event, use useCallback.

If component unmounts before setState, it wont break your app but will get an error in console as set state was called in unmounted component. Optionally to avoid this you can cancel api call when you component unmounts.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top