useIsMountedRef.tsx 449 B

123456789101112131415161718192021
  1. import {useEffect, useRef} from 'react';
  2. /**
  3. * Returns a ref that captures the current mounted state of the component
  4. *
  5. * This hook is handy for the cases when you have to detect component mount state
  6. * within async effects.
  7. */
  8. export function useIsMountedRef() {
  9. const isMounted = useRef(false);
  10. useEffect(() => {
  11. isMounted.current = true;
  12. return () => {
  13. isMounted.current = false;
  14. };
  15. }, []);
  16. return isMounted;
  17. }