Skip to content

Commit

Permalink
chore: little cleanup for the hook
Browse files Browse the repository at this point in the history
  • Loading branch information
ovflowd committed Dec 28, 2024
1 parent 15fbb89 commit 142a02a
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions apps/site/hooks/react-client/useMediaQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@

import { useState, useEffect } from 'react';

const useMediaQuery = (query: string): boolean | undefined => {
const useMediaQuery = (query: string) => {
const [matches, setMatches] = useState<boolean>();

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;
Expand Down

0 comments on commit 142a02a

Please sign in to comment.