Stop checking for window in useEffect

This commit is contained in:
Sam Becker 2025-02-09 09:54:17 -06:00
parent 6cefb12f30
commit 3c04ca840f

View File

@ -4,18 +4,16 @@ export default function useIsDesktop() {
const [isDesktop, setIsDesktop] = useState<boolean>();
useEffect(() => {
if (typeof window !== 'undefined') {
const breakpointMd = getComputedStyle(document.body)
.getPropertyValue('--breakpoint-md');
const mql = window.matchMedia(`(min-width: ${breakpointMd})`);
setIsDesktop(mql.matches);
const breakpointMd = getComputedStyle(document.body)
.getPropertyValue('--breakpoint-md');
const mql = window.matchMedia(`(min-width: ${breakpointMd})`);
setIsDesktop(mql.matches);
const eventHandler = (event: MediaQueryListEvent) =>
setIsDesktop(event.matches);
const eventHandler = (event: MediaQueryListEvent) =>
setIsDesktop(event.matches);
mql.addEventListener('change', eventHandler);
return () => mql.removeEventListener('change', eventHandler);
}
mql.addEventListener('change', eventHandler);
return () => mql.removeEventListener('change', eventHandler);
}, []);
return isDesktop;