Part of the Ant Design ecosystem.
๐๏ธ React wrapper and hook for observing DOM mutations with a typed MutationObserver API.
English | ็ฎไฝไธญๆ
- Component and hook APIs for observing changes to a rendered DOM node.
- Passes
MutationRecord[]and the nativeMutationObserverinstance toonMutate. - Supports the full native
MutationObserverInitoptions object. - TypeScript definitions for component props and hook usage.
- Used by Ant Design as a small DOM observation primitive.
npm install @rc-component/mutate-observerimport MutateObserver from '@rc-component/mutate-observer';
export default () => (
<MutateObserver
options={{ attributes: true, childList: true, subtree: true }}
onMutate={(mutations, observer) => {
console.log(mutations);
console.log(observer);
}}
>
<div>Observed content</div>
</MutateObserver>
);import { useMutateObserver } from '@rc-component/mutate-observer';
export default ({ target }: { target: HTMLDivElement | null }) => {
useMutateObserver(target, mutations => {
console.log(mutations);
});
return <div>Observed content</div>;
};Run the local dumi site:
npm install
npm startThen open http://localhost:8000.
| Property | Type | Default | Description |
|---|---|---|---|
| children | React.ReactNode |
- | Node to render and observe. |
| options | MutationObserverInit |
- | Native observer options such as attributes, childList, and subtree. |
| onMutate | (mutations: MutationRecord[], observer: MutationObserver) => void |
- | Triggered when observed mutations are reported. |
useMutateObserver(target, callback, options);The hook observes the provided target element or element array with the same callback and native MutationObserverInit options used by the component.
npm install
npm start
npm test
npm run tsc
npm run compile
npm run buildThe dumi site runs at http://localhost:8000 by default.
npm run prepublishOnlyThe release flow is handled by @rc-component/np through the rc-np command after the package build.
@rc-component/mutate-observer is released under the MIT license.