Skip to content

Commit

Permalink
Merge pull request #2536 from willnguyen1312/pr/update-use-media
Browse files Browse the repository at this point in the history
fix: update useMedia hook to use recommended approach of MDN
  • Loading branch information
streamich committed Jan 13, 2024
2 parents e27c193 + e7379f0 commit 3264d96
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/useMedia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ const useMedia = (query: string, defaultState?: boolean) => {
setState(!!mql.matches);
};

mql.addListener(onChange);
mql.addEventListener('change', onChange);
setState(mql.matches);

return () => {
mounted = false;
mql.removeListener(onChange);
mql.removeEventListener('change', onChange);
};
}, [query]);

Expand Down
4 changes: 2 additions & 2 deletions tests/useMedia.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { useMedia } from '../src';

const createMockMediaMatcher = (matches: Record<string, boolean>) => (qs: string) => ({
matches: matches[qs] ?? false,
addListener: () => {},
removeListener: () => {},
addEventListener: () => {},
removeEventListener: () => {},
});

describe('useMedia', () => {
Expand Down

0 comments on commit 3264d96

Please sign in to comment.