UNPKG

@react-hookz/web

Version:

React hooks done right, for browser and SSR.

28 lines (27 loc) 961 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useIsMounted = void 0; var react_1 = require("react"); /** * Returns function that yields current mount state. * * Returned function yields `true` only in case component is mounted. This hook * is handy for the cases when you have to detect component mount state within * async effects. * * @param initialValue Initial value. By default, this hook assumes that hook is * not mounted yet at first render. */ function useIsMounted(initialValue) { if (initialValue === void 0) { initialValue = false; } var isMounted = (0, react_1.useRef)(initialValue); var get = (0, react_1.useCallback)(function () { return isMounted.current; }, []); (0, react_1.useEffect)(function () { isMounted.current = true; return function () { isMounted.current = false; }; }, []); return get; } exports.useIsMounted = useIsMounted;