BlockDex

Components

Registries

Free blocks

Method

BlockDex

Components

Registries

Free blocks

Method

Open API

BlockDex

Components

Registries

Free blocks

Method

Open API

Components/guarahooks/use-mutation-observer

UseMutationObserver

guarahooks/use-mutation-observer
FreeHook

Observes changes to the DOM using the Mutation Observer API

Live preview

live · rendered by the registry
Rendered by guarahooks on their own page — this is the component running, not a screenshot of it.

Install it

npx shadcn@latest add https://guarahooks.com/r/use-mutation-observer.json

Source

registry/hooks/use-mutation-observer.tsxguarahooks.com/r/use-mutation-observer.json
1'use client';
2
3import { useCallback, useEffect, useRef } from 'react';
4
5// Types
6type UseMutationObserverProps<T extends HTMLElement = HTMLElement> = {
7 target: React.RefObject<T | null>;
8 options?: MutationObserverInit;
9 callback: MutationCallback;
10};
11
12export function useMutationObserver<T extends HTMLElement = HTMLElement>({
13 target,
14 options = {
15 attributes: true,
16 childList: true,
17 subtree: true,
18 },
19 callback,
20}: UseMutationObserverProps<T>) {
21 const observerRef = useRef<MutationObserver | null>(null);
22 const prevTargetRef = useRef<T | null>(null);
23
24 const disconnect = useCallback(() => {
25 if (observerRef.current) {
26 observerRef.current.disconnect();
27 observerRef.current = null;
28 }
29 }, []);
30
31 useEffect(() => {
32 const node = target.current;
33 if (!node) return;
34
35 if (prevTargetRef.current !== node) {
36 disconnect();
37 prevTargetRef.current = node;
38 }
39
40 observerRef.current = new MutationObserver(callback);
41 observerRef.current.observe(node, options);
42 return () => {
43 disconnect();
44 };
45 }, [target, options, callback, disconnect]);
46
47 return {
48 disconnect,
49 };
50}

Files it writes

  • registry/hooks/use-mutation-observer.tsx

Facts

Registry
guarahooks
Type
registry:hook
Access
Free
Files written
1
Licence
MIT
First indexed
2026-08-03
Last confirmed
yesterday

Go to the source

Docs on guarahooks guarahooks.com h3rmel/guarahooks on GitHub Raw registry item This item as JSON

More from guarahooks

All 100 items
Same registry, same kind — the ones you would have found next
ComponentRegistryKindAccessInstallsCommand
UseAbortControlleruse-abort-controllerguarahooksHooksFreeno deps
UseArrayStateuse-array-stateguarahooksHooksFreeno deps
UseAxiosuse-axiosguarahooksHooksFreeno deps
UseBatteryStatususe-battery-statusguarahooksHooksFreeno deps
UseBetterAuthuse-better-authguarahooksHooksFreeno deps
UseClickOutsideuse-click-outsideguarahooksHooksFreeno deps
UseConfirmuse-confirmguarahooksHooksFreeno deps
UseCookieuse-cookieguarahooksHooksFreeno deps
BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex

BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex

BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex