h3/use's logo

h3/use

Command Palette

Search for a command to run...

useMutationObserver

A React hook for observing changes to the DOM using the Mutation Observer API

Mutation Observer Demo
Watch how the hook detects DOM changes in real-time

Target Element

Mutation Count: 0

Installation

Props

PropTypeDefaultDescription
targetReact.RefObject<T | null>-The DOM element to observe
optionsMutationObserverInitSee belowConfiguration for the mutation observer
callbackMutationCallback-Function called when mutations are observed

Options

PropTypeDefaultDescription
attributesbooleantrueWhether to observe attribute changes
childListbooleantrueWhether to observe child node changes
subtreebooleantrueWhether to observe changes in the entire subtree of the target element

Data

The hook returns an object with the following properties:

PropertyTypeDescription
disconnect() => voidFunction to disconnect the mutation observer

Key Features & Details

Browser Support

The Mutation Observer API is well-supported across modern browsers:

  • Chrome: Full support
  • Firefox: Full support
  • Safari: Full support
  • Edge: Full support
  • Opera: Full support

Cross-Browser Compatibility

  • The hook handles browser-specific implementations and edge cases
  • Provides consistent behavior across different browsers
  • Gracefully handles cases where the API might not be fully supported

Performance Considerations

  • The hook automatically disconnects observers when components unmount
  • Provides a disconnect function for manual cleanup
  • Supports configurable observation options to optimize performance
  • Handles edge cases where the target element might be null

Best Practices & Caveats

  1. Always provide a target element
  2. Use the disconnect function when you no longer need to observe changes
  3. Be mindful of performance when observing large subtrees
  4. Consider using specific options to limit the types of mutations you observe
  5. Clean up observers when components unmount
  6. Use appropriate options based on your use case
  7. Handle edge cases where the target element might be null
  8. Consider debouncing or throttling the callback for performance

Examples

Basic Usage

const [mutations, setMutations] = useState<MutationRecord[]>([]);
const targetRef = useRef<HTMLDivElement | null>(null);
 
useMutationObserver<HTMLDivElement>({
  target: targetRef,
  callback: (mutationsList) => {
    setMutations((prev) => [...prev, ...mutationsList]);
  },
});
 
return <div ref={targetRef}>Target Element</div>;

With Custom Options

useMutationObserver<HTMLDivElement>({
  target: targetRef,
  callback: (mutationsList) => {
    // Handle mutations
  },
  options: {
    attributes: true,
    childList: false,
    subtree: false,
  },
});

With Disconnect

const { disconnect } = useMutationObserver<HTMLDivElement>({
  target: targetRef,
  callback: (mutationsList) => {
    // Handle mutations
  },
});
 
// Later, when you want to stop observing
disconnect();

Usage

import { useMutationObserver } from '@/hooks/use-mutation-observer';
 
function MyComponent() {
  const targetRef = useRef<HTMLDivElement | null>(null);
  const [mutations, setMutations] = useState<MutationRecord[]>([]);
 
  useMutationObserver<HTMLDivElement>({
    target: targetRef,
    callback: (mutationsList) => {
      setMutations((prev) => [...prev, ...mutationsList]);
    },
  });
 
  return <div ref={targetRef}>Target Element</div>;
}

Common Use Cases

  1. Dynamic Content Loading: Observe when new content is added to a container
  2. Form Validation: Watch for attribute changes on form elements
  3. UI State Management: Track changes to UI elements for state synchronization
  4. Third-party Integration: Monitor changes made by third-party scripts
  5. Accessibility: Track changes that might affect accessibility