useMutationObserver
Reactive wrapper around the MutationObserver API.
Observes one or more DOM nodes for mutations — attribute changes, child additions/removals, and text content changes.
Targets can be Ref$, MaybeElement, or a plain Element.
import { function useRef$<T extends Element = Element>(externalRef?: React.Ref<T> | null): Ref$<T>
Creates an observable element ref. Can be used as a drop-in replacement for
useRef, composed with callback refs, or used with forwardRef.
The element is wrapped with opaqueObject to prevent legendapp/state
from making DOM properties reactive (deep observation).
useRef$, import useMutationObserver
useMutationObserver } from "@usels/core";function function Component(): React.JSX.Element
Component() { const const el$: Ref$<HTMLDivElement>
el$ = useRef$<HTMLDivElement>(externalRef?: React.Ref<HTMLDivElement> | undefined): Ref$<HTMLDivElement>
Creates an observable element ref. Can be used as a drop-in replacement for
useRef, composed with callback refs, or used with forwardRef.
The element is wrapped with opaqueObject to prevent legendapp/state
from making DOM properties reactive (deep observation).
useRef$<interface HTMLDivElement
HTMLDivElement>();
import useMutationObserver
useMutationObserver( const el$: Ref$<HTMLDivElement>
el$, (records: any
records) => { records: any
records.any
forEach((r: any
r) => { /* handle r.type, r.target */ }); }, { attributes: boolean
attributes: true, childList: boolean
childList: true } );
return <React.JSX.IntrinsicElements.div: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div React.RefAttributes<HTMLDivElement>.ref?: React.Ref<HTMLDivElement> | undefined
Allows getting a ref to the component instance.
Once the component unmounts, React will set ref.current to null
(or call the ref with null if you passed a callback ref).
ref={const el$: Ref$<HTMLDivElement>
el$} />;}Watching attributes only
Section titled “Watching attributes only”useMutationObserver(el$, callback, { attributes: true });Filtering specific attributes
Section titled “Filtering specific attributes”Only fire when aria-expanded or data-active change:
useMutationObserver(el$, callback, { attributes: true, attributeFilter: ["aria-expanded", "data-active"],});Recording the previous attribute value
Section titled “Recording the previous attribute value”useMutationObserver( el$, (records) => { records.forEach((r) => { const next = (r.target as Element).getAttribute(r.attributeName!); console.log("old:", r.oldValue, "→ new:", next); }); }, { attributes: true, attributeOldValue: true });Watching descendant nodes with subtree
Section titled “Watching descendant nodes with subtree”useMutationObserver(el$, callback, { childList: true, subtree: true });Multiple targets
Section titled “Multiple targets”useMutationObserver([el$, anotherEl], callback, { attributes: true });Stop and resume
Section titled “Stop and resume”const { stop, resume } = useMutationObserver(el$, callback, { childList: true });
stop(); // disconnects the observerresume(); // reconnects with the same target and optionsFlushing pending records
Section titled “Flushing pending records”const { takeRecords } = useMutationObserver(el$, callback, { attributes: true });
const pending = takeRecords();Checking browser support
Section titled “Checking browser support”const { isSupported$ } = useMutationObserver(el$, callback, { attributes: true });
console.log(isSupported$.get()); // Observable<boolean>Type Declarations
Section titled “Type Declarations”export interface UseMutationObserverOptions extends MutationObserverInit {}export interface UseMutationObserverReturn { isSupported$: Observable<boolean>; stop: () => void; resume: () => void; takeRecords: () => MutationRecord[];}export declare function useMutationObserver(target: MaybeElement | MaybeElement[], callback: MutationCallback, options?: UseMutationObserverOptions): UseMutationObserverReturn;Source
Section titled “Source”Contributors
Section titled “Contributors”- tigerwest
Changelog
Section titled “Changelog”a7392ab2026-03-06 - feat(core,browser): add sync strategy hooks (tigerwest)