R
fn

useMutationObserver

v0.0.15testeddemo

Watch for changes to the DOM tree via `MutationObserver`. Accepts a single target, an array of targets, or a getter returning either.

Examples

ts
useMutationObserver(el, (records) => {
  console.log(records);
}, { attributes: true });
ts
const { pause, resume } = useMutationObserver([elA, elB], onMutate, { childList: true });

Demo

Loading demo…

Signature

ts
export function useMutationObserver(
  target: MaybeComputedElementRef | MaybeComputedElementRef[] | MaybeRefOrGetter<MaybeElement[]>,
  callback: MutationCallback,
  options: UseMutationObserverOptions ={ ... }

Parameters

ParameterTypeDescription
targetanyElement(s) to observe
callbackMutationCallbackInvoked with the mutation records
options?UseMutationObserverOptionsObserver options (childList, attributes, …)

Returns

UseMutationObserverReturn`isSupported`, `isActive`, `pause`, `resume`, `stop`, and `takeRecords`