Skip to content

Instantly share code, notes, and snippets.

@theluk
Created July 11, 2021 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theluk/e748734d84031c3aa85af469d63dcedc to your computer and use it in GitHub Desktop.
Save theluk/e748734d84031c3aa85af469d63dcedc to your computer and use it in GitHub Desktop.
useMountedEffect.ts

Asynchronous methods within hooks can lead to issues, where your asynchronous code resolves after your component / hook was unmounted.

This usually causes react warnings, that you cannot set state on an unmounted component.

How to use useMountedEffect

const MyComponent = ({ id }) => {
  const [status, setStatus] = useState();
  const [data, setData] = useState();
  
  useMountedEffect(async (isMounted) => {
    setStatus("LOADIGN");
    try {
      const data = await fetchMyData({ id });
      // only set state if we are still mounted
      if (isMounted()) {
        setData(data);
        setStatus("DONE")
      }
    } catch (e) {
      if (isMounted()) {
        setStatus("ERROR")
      }
    }
  }, [id])
}
import { useEffect, useRef, DependencyList } from 'react';
export default function useMountedEffect(
callback: (isMounted: () => boolean) => void,
deps?: DependencyList | undefined,
): void {
const callbackRef = useRef(callback);
callbackRef.current = callback;
const mountedRef = useRef(false);
useEffect(() => {
mountedRef.current = true;
callbackRef.current(() => mountedRef.current);
return () => {
mountedRef.current = false;
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, deps);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment