Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/wojtekmaj/react-hooks. A collection of React Hooks.
hooks react react-hooks
Something went wrong. Try again.
953 B · 31 lines
TypeScript
at main
1234567891011121314151617181920212223242526272829303132import { useEffect } from 'react';
/** * Observes a given element using MutationObserver. * * @param {Node} [element] Element to attach MutationObserver to * @param {MutationObserverInit} [options] MutationObserver options. WARNING! If you define the * object in component body, make sure to memoize it. * @param {MutationCallback} observerCallback MutationObserver callback. WARNING! If you define the * function in component body, make sure to memoize it. * @returns {void} */export default function useMutationObserver( element: Node | null, options: MutationObserverInit, observerCallback: MutationCallback,): void { useEffect(() => { if (!element || !('MutationObserver' in window)) { return undefined; }
const observer = new MutationObserver(observerCallback);
observer.observe(element, options);
return () => { observer.disconnect(); }; }, [element, options, observerCallback]);}