Skip to Content
react-utilsuseMutationObserver

useMutationObserver

See source on Github

Registers a MutationObserver on ref and executes callback when the observed element is mutated.

  • @param ref - A RefObject of the element to observe.
  • @param callback - A function to execute when the observed element is mutated.
  • @param options - Configurable options
  • @returns void

Example

const ref = useRef<HTMLDivElement>(null); const [isLoading, setIsLoading] = useState(false); useMutationObserver(ref, (record) => { setIsLoading(record.attributeName); }, { attributes: true, attributeFilter: ['data-loading'] });