UNPKG

@huse/effect-ref

Version:

--- title: README nav: title: Hooks path: /hook group: title: Effect Ref path: /effect-ref order: 1 ---

24 lines 928 B
import { useRef, useCallback } from 'react'; // eslint-disable-next-line @typescript-eslint/no-empty-function const noop = () => { }; export function useEffectRef(callback) { const disposeRef = useRef(noop); const effect = useCallback((element) => { disposeRef.current(); // To ensure every dispose function is called only once. disposeRef.current = noop; if (element) { const dispose = callback(element); if (typeof dispose === 'function') { disposeRef.current = dispose; } // Have an extra type check to work with javascript. else if (dispose !== undefined) { // eslint-disable-next-line no-console console.warn('Effect ref callback must return undefined or a dispose function'); } } }, [callback]); return effect; } //# sourceMappingURL=index.js.map