useMutationObserver
API
import useMutationObserver from '@restart/hooks/useMutationObserver'
useMutationObserver
(element: Element | null | undefined, config: MutationObserverInit, callback: MutationCallback) => voidObserve mutations on a DOM node or tree of DOM nodes. Depends on the
MutationObserver
api.const [element, attachRef] = useCallbackRef(null);useMutationObserver(element, { subtree: true }, (records) => {});return (<div ref={attachRef} />)Parameters
element
Element | null | undefinedThe DOM element to observe
config
MutationObserverInitThe observer configuration
callback
MutationCallbackA callback fired when a mutation occurs
Return Value void
useMutationObserver
(element: Element | null | undefined, config: MutationObserverInit) => MutationRecord[]Observe mutations on a DOM node or tree of DOM nodes. use a
MutationObserver
and return records as the are received.const [element, attachRef] = useCallbackRef(null);const records = useMutationObserver(element, { subtree: true });return (<div ref={attachRef} />)Parameters
element
Element | null | undefinedThe DOM element to observe
config
MutationObserverInitThe observer configuration
Return Value MutationRecord[]