Skip to content

Commit

Permalink
Correct dependencies of useMemo and useEffect in useVisible
Browse files Browse the repository at this point in the history
  • Loading branch information
jb3 committed Sep 11, 2024
1 parent 00e71b8 commit 1b5a4f5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion thallium-frontend/src/components/CartStatus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ transition: all 0.25s;
${Card} {
box-shadow: none;
}
`
`;

const FloatingButton = styled(Button) <{ $visible: boolean }>`
position: fixed;
Expand Down
14 changes: 7 additions & 7 deletions thallium-frontend/src/utils/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { useState, useEffect, useMemo } from "react";
import { type RefObject } from "react";

export function useVisible(ref: RefObject<HTMLElement>, initialState: boolean = false) {
export function useVisible(ref: RefObject<HTMLElement>, initialState = false) {
const [isVisible, setVisible] = useState(initialState);

const intersectionObserver = useMemo(() => {
return new IntersectionObserver(
([entry]) => { setVisible(entry.isIntersecting) },
([entry]) => { setVisible(entry.isIntersecting); },
{
threshold: 0.5
}
)
}, [ref]);
);
}, []);


useEffect(() => {
if (ref.current)
intersectionObserver.observe(ref.current);

return () => { intersectionObserver.disconnect(); };
}, [ref]);
}, [ref, intersectionObserver]);

return isVisible;
}
Expand All @@ -34,11 +34,11 @@ export function useMediaQuery(query: string) {
useEffect(() => {
const listenHook = (e: MediaQueryListEvent) => {
setMatches(e.matches);
}
};

queryObject.addEventListener("change", listenHook);

return () => queryObject.removeEventListener("change", listenHook);
return () => { queryObject.removeEventListener("change", listenHook); };
}, [queryObject]);

return matches;
Expand Down

0 comments on commit 1b5a4f5

Please sign in to comment.