useMutationObserver
Observes DOM tree, attribute, or character data mutations on a target node using MutationObserver.
Loading preview...
The useMutationObserver hook attaches a MutationObserver to a target DOM node and invokes a callback whenever matching attribute, child list, or character data mutations occur.
Installation
Usage
Examples
Watching data-* or aria-* Attribute Changes
Configure attributes: true and attributeFilter to react only when specific attributes change on a target element.
Pausing Observation with enabled
Set enabled: false in the options object to temporarily disconnect the MutationObserver.
API Reference
UseMutationObserverOptions
Configuration options extending standard MutationObserverInit with an enabled flag.
Property
Specification
enabled?uniondefault:true
Whether the mutation observer is active
Full Signature
attributeFilter?array
Set to a list of attribute local names (without namespace) if not all attribute mutations need to be observed and attributes is true or omitted.
Full Signature
attributeOldValue?union
Set to true if attributes is true or omitted and target's attribute value before the mutation needs to be recorded.
Full Signature
attributes?union
Set to true if mutations to target's attributes are to be observed. Can be omitted if attributeOldValue or attributeFilter is specified.
Full Signature
characterData?union
Set to true if mutations to target's data are to be observed. Can be omitted if characterDataOldValue is specified.
Full Signature
characterDataOldValue?union
Set to true if characterData is set to true or omitted and target's data before the mutation needs to be recorded.
Full Signature
childList?union
Set to true if mutations to target's children are to be observed.
Full Signature
subtree?union
Set to true if mutations to not just target, but also target's descendants are to be observed.
Full Signature