UNPKG

@supunlakmal/hooks

Version:

A collection of reusable React hooks

21 lines 691 B
import { useCallback, useEffect, useRef } from 'react'; /** * Returns a function that returns the current mount state. This hook is useful when you have to * detect component mount state within async effects. * * @param initialValue Initial value. * * @return Function that returns `true` only if the component is mounted. */ export const useIsMounted = (initialValue = false) => { const isMounted = useRef(initialValue); const get = useCallback(() => isMounted.current, []); useEffect(() => { isMounted.current = true; return () => { isMounted.current = false; }; }, []); return get; }; //# sourceMappingURL=useIsMounted.js.map