UNPKG

atechhub-use-mounted

Version:

A React hook to track component mounted state

21 lines (20 loc) 548 B
import { useEffect, useRef } from "react"; /** * useMounted is a custom hook that tracks the mounted state of a component. * It returns a ref object with a boolean value indicating whether the component is mounted. * * @returns {Object} An object containing the mounted ref */ const useMounted = () => { const mounted = useRef(false); useEffect(() => { mounted.current = true; return () => { mounted.current = false; }; }, []); return { mounted, }; }; export default useMounted;