Skip to content

Commit

Permalink
refactor: update media query event listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
adamalston committed Jan 18, 2025
1 parent c0f4be4 commit 9780737
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/App/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ export const App = () => {
const [isMobile, setIsMobile] = useState(false);

useEffect(() => {
const updateIsMobile = () => {
const { matches } = matchMedia(
'(max-device-width: 820px) and (-webkit-min-device-pixel-ratio: 2)',
);
const mediaQuery =
'(max-device-width: 820px) and (-webkit-min-device-pixel-ratio: 2)';
const mediaQueryList = window.matchMedia(mediaQuery);

setIsMobile(matches);
const updateIsMobile = () => {
setIsMobile(mediaQueryList.matches);
};

updateIsMobile();

window.addEventListener('resize', updateIsMobile);
mediaQueryList.addEventListener('change', updateIsMobile);

return () => {
window.removeEventListener('resize', updateIsMobile);
mediaQueryList.removeEventListener('change', updateIsMobile);
};
}, []);

Expand Down

0 comments on commit 9780737

Please sign in to comment.