useMutationObserver
dependencies
code:script.js
import { useEffect } from '../preact@10.5.13/hooks.js';
export const useMutationObserver = (elements, callback, config) =>
useEffect(() => {
const mutationObserver = new MutationObserver((mutations) => {
mutationObserver.disconnect();
callback(mutations);
for (const elem of elements) {
elem.current && mutationObserver.observe(elem.current, config);
}
});
for (const elem of elements) {
elem.current && mutationObserver.observe(elem.current, config);
}
return () => mutationObserver.disconnect();
}, []);