From 142a02a2006a5e98baaef939e28fe0b83b0e10b4 Mon Sep 17 00:00:00 2001 From: Claudio Wunder Date: Sat, 28 Dec 2024 21:16:11 +0000 Subject: [PATCH] chore: little cleanup for the hook --- apps/site/hooks/react-client/useMediaQuery.ts | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/apps/site/hooks/react-client/useMediaQuery.ts b/apps/site/hooks/react-client/useMediaQuery.ts index 5b4a15a42fd1d..0c8a1625cea8a 100644 --- a/apps/site/hooks/react-client/useMediaQuery.ts +++ b/apps/site/hooks/react-client/useMediaQuery.ts @@ -2,23 +2,20 @@ import { useState, useEffect } from 'react'; -const useMediaQuery = (query: string): boolean | undefined => { +const useMediaQuery = (query: string) => { const [matches, setMatches] = useState(); useEffect(() => { - if (typeof window?.matchMedia === 'function') { - const mq = window.matchMedia(query); - setMatches(mq.matches); + const mq = window.matchMedia(query); - const handler = (event: MediaQueryListEvent): void => - setMatches(event.matches); + setMatches(mq.matches); - mq.addEventListener('change', handler); + const handler = (event: MediaQueryListEvent): void => + setMatches(event.matches); - return (): void => mq.removeEventListener('change', handler); - } + mq.addEventListener('change', handler); - return undefined; + return () => mq.removeEventListener('change', handler); }, [query]); return matches;