UNPKG

@selfcommunity/react-core

Version:

React Core Components useful for integrating UI Community components (react-ui).

37 lines (36 loc) 1.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const react_1 = require("react"); /** * @param effect * @param dependancies * @description Hook to prevent running the useEffect on the first render */ const useNoInitialEffect = (effect, dependancies) => { // Preserving the true by default as initial render cycle const initialRender = (0, react_1.useRef)(true); (0, react_1.useEffect)(() => { // eslint-disable-next-line @typescript-eslint/no-empty-function let effectReturns = () => { }; /** * Updating the ref to false on the first render, causing * subsequent render to execute the effect */ if (initialRender.current) { initialRender.current = false; } else { effectReturns = effect(); } /** * Preserving and allowing the Destructor returned by the effect * to execute on component unmount and perform cleanup if * required. */ if (effectReturns && typeof effectReturns === 'function') { return effectReturns; } return undefined; }, dependancies); }; exports.default = useNoInitialEffect;