useMutationObserver

    API

    import useMutationObserver from '@restart/hooks/useMutationObserver'
    • useMutationObserver(element: Element | null | undefined, config: MutationObserverInit, callback: MutationCallback) => void

      Observe 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

      • elementElement | null | undefined

        The DOM element to observe

      • configMutationObserverInit

        The observer configuration

      • callbackMutationCallback

        A 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

      • elementElement | null | undefined

        The DOM element to observe

      • configMutationObserverInit

        The observer configuration

      Return Value MutationRecord[]