Files
shadcn-ui/examples/playground/hooks/use-mutation-observer.ts
2023-02-13 21:28:40 +04:00

21 lines
428 B
TypeScript

import * as React from "react"
export const useMutationObserver = (
ref,
callback,
options = {
attributes: true,
characterData: true,
childList: true,
subtree: true,
}
) => {
React.useEffect(() => {
if (ref.current) {
const observer = new MutationObserver(callback)
observer.observe(ref.current, options)
return () => observer.disconnect()
}
}, [ref, callback, options])
}