Overview
Observes DOM mutations on the element attached through the returned ref. The observer is recreated when dependencies change and is disconnected during cleanup.Function Signature
Parameters
Mutation observer callback invoked with mutation records. Receives two arguments:
mutations: Array ofMutationRecordobjects describing changesobserver: TheMutationObserverinstance itself
Native
MutationObserver options for what to observe. Must include at least one of:childList: Set totrueto observe additions/removals of child nodesattributes: Set totrueto observe attribute changescharacterData: Set totrueto observe text content changessubtree: Set totrueto observe descendants in addition to targetattributeOldValue: Set totrueto record attribute’s previous valuecharacterDataOldValue: Set totrueto record character data’s previous valueattributeFilter: Array of attribute names to observe (ifattributesis true)
Return Value
A ref callback function to attach to your React element. The mutation observer will monitor the element according to the provided options.
Usage Example
Observing Attributes
useMutationObserverTarget
Observes DOM mutations for a specific target element without using a ref.Function Signature
Parameters
Mutation observer callback invoked with mutation records.
Native
MutationObserver options for what to observe.Target element (or target resolver function) to observe. If a function, it will be called to get the target element.
Usage Example
Features
- Uses native
MutationObserverAPI for efficient DOM monitoring - Automatic observer cleanup when element changes or component unmounts
- Supports all
MutationObserverconfiguration options - Alternative hook for observing specific targets without refs
- Type-safe mutation callbacks