useMutationObserver

Observe changes to the DOM tree.

Usage Example
1import { useMutationObserver } from '@danixsoft/hooks';
2import { useRef, useState } from 'react';
3
4function Example() {
5  const ref = useRef<HTMLDivElement>(null);
6  const [mutations, setMutations] = useState(0);
7
8  useMutationObserver(
9    ref,
10    (mutationList) => {
11      setMutations((m) => m + mutationList.length);
12    },
13    { childList: true, subtree: true }
14  );
15
16  return (
17    <div>
18      <div ref={ref}>
19        <button onClick={() => ref.current?.append(document.createElement('div'))}>
20          Mutate DOM
21        </button>
22      </div>
23      <p>Mutations count: {mutations}</p>
24    </div>
25  );
26}