UNPKG

react-use-mounted

Version:

React hook for checking if the component is mounted.

32 lines (16 loc) 380 B
/* IMPORT */ import {useEffect, useRef} from 'react'; import type {RefObject} from 'react'; /* MAIN */ const useMounted = (): RefObject<boolean> => { const mounted = useRef ( false ); useEffect ( () => { mounted.current = true; return () => { mounted.current = false; }; }, [] ); return mounted; }; /* EXPORT */ export default useMounted;