Skip to content

Commit

Permalink
Move Mutation observer creation inside useEffect
Browse files Browse the repository at this point in the history
(So it works in SSR)
  • Loading branch information
tmeasday committed Dec 4, 2022
1 parent 74f795f commit ab4855e
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions code/ui/components/src/Zoom/ZoomElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ const useMutationObserver = ({
options: MutationObserverInit;
callback: MutationCallback;
}): void => {
const observer = useMemo(
() =>
new MutationObserver((mutationRecord, mutationObserver) => {
callback(mutationRecord, mutationObserver);
}),
[callback]
);
const observer = useRef<MutationObserver>();

useEffect(() => {
if (!observer.current) {
observer.current = new MutationObserver((mutationRecord, mutationObserver) => {
callback(mutationRecord, mutationObserver);
});
}

if (element?.current) {
observer.observe(element.current, options);
observer.current.observe(element.current, options);
}

return () => observer.disconnect();
return () => observer.current.disconnect();
}, [element, observer, options]);
};

Expand Down

0 comments on commit ab4855e

Please sign in to comment.