Skip to content

Commit

Permalink
fix: update useMedia hook to use recommended approach of MDN
Browse files Browse the repository at this point in the history
  • Loading branch information
willnguyen1312 committed Jan 10, 2024
1 parent e27c193 commit e7379f0
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 e7379f0

Please sign in to comment.