aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Joe Banks <[email protected]>2024-09-11 23:31:57 +0100
committerGravatar Joe Banks <[email protected]>2024-09-11 23:31:57 +0100
commit1b5a4f517f554eecc2a7b1a320bac2f141a513e3 (patch)
tree902f2dcffcf5b533bf7aece46e3a4c1de8addd1f
parentAllow for seeding useVisible with preferred default (diff)
Correct dependencies of useMemo and useEffect in useVisible
-rw-r--r--thallium-frontend/src/components/CartStatus.tsx2
-rw-r--r--thallium-frontend/src/utils/hooks.ts14
2 files changed, 8 insertions, 8 deletions
diff --git a/thallium-frontend/src/components/CartStatus.tsx b/thallium-frontend/src/components/CartStatus.tsx
index d65697a..cdb35f3 100644
--- a/thallium-frontend/src/components/CartStatus.tsx
+++ b/thallium-frontend/src/components/CartStatus.tsx
@@ -22,7 +22,7 @@ transition: all 0.25s;
${Card} {
box-shadow: none;
}
-`
+`;
const FloatingButton = styled(Button) <{ $visible: boolean }>`
position: fixed;
diff --git a/thallium-frontend/src/utils/hooks.ts b/thallium-frontend/src/utils/hooks.ts
index 79486a3..c68a4a6 100644
--- a/thallium-frontend/src/utils/hooks.ts
+++ b/thallium-frontend/src/utils/hooks.ts
@@ -1,17 +1,17 @@
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(() => {
@@ -19,7 +19,7 @@ export function useVisible(ref: RefObject<HTMLElement>, initialState: boolean =
intersectionObserver.observe(ref.current);
return () => { intersectionObserver.disconnect(); };
- }, [ref]);
+ }, [ref, intersectionObserver]);
return isVisible;
}
@@ -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;