Skip to content

fix(useMediaQuery): remove asymmetric addListener/removeListener fallback in unsubscribe#1663

Open
lennondotw wants to merge 1 commit intoreact-hookz:masterfrom
lennondotw:fix/compat
Open

fix(useMediaQuery): remove asymmetric addListener/removeListener fallback in unsubscribe#1663
lennondotw wants to merge 1 commit intoreact-hookz:masterfrom
lennondotw:fix/compat

Conversation

@lennondotw
Copy link
Copy Markdown

@lennondotw lennondotw commented Oct 23, 2025

Summary

createQueryEntry uses addEventListener unconditionally (as of #1661), but queryUnsubscribe still had a removeEventListener/removeListener fallback for Safari < 14. This asymmetry is unnecessary — if subscribe doesn't handle old browsers, unsubscribe shouldn't either.

Changes

  • Removed the removeEventListener/removeListener conditional in queryUnsubscribe, now uses removeEventListener directly to match createQueryEntry

@lennondotw lennondotw changed the title fix(useMediaQuery): restore addEventListener compatibility check for symmetry Fix compatibility issue for useMediaQuery Oct 23, 2025
@xobotyi xobotyi self-assigned this Jan 11, 2026
@xobotyi xobotyi requested a review from Copilot January 11, 2026 16:44
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a compatibility issue in the useMediaQuery hook where event listener subscription and unsubscription were handled asymmetrically, causing errors in Safari versions < 14 that only support the deprecated addListener/removeListener methods.

Changes:

  • Restored the addEventListener/addListener compatibility check in the createQueryEntry function to match the existing compatibility logic in queryUnsubscribe
  • Ensures symmetric handling of event listeners for both subscription and unsubscription operations

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +20 to +24
if (mql.addEventListener) {
mql.addEventListener('change', listener, {passive: true});
} else {
mql.addListener(listener);
}
Copy link

Copilot AI Jan 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding test coverage for the legacy addListener/removeListener compatibility path. The current tests only mock addEventListener and removeEventListener. Adding a test that mocks a MediaQueryList without addEventListener would ensure this compatibility code path is tested.

Copilot uses AI. Check for mistakes.
…back in unsubscribe

createQueryEntry uses addEventListener unconditionally, but
queryUnsubscribe still had a removeEventListener/removeListener
fallback. This asymmetry is unnecessary since all supported
browsers now have addEventListener on MediaQueryList.
@lennondotw lennondotw changed the title Fix compatibility issue for useMediaQuery fix(useMediaQuery): remove asymmetric addListener/removeListener fallback in unsubscribe Apr 3, 2026
@lennondotw
Copy link
Copy Markdown
Author

Rebased on latest master and updated the fix — the original PR was adding back the addListener fallback, but since #1661 intentionally removed it from createQueryEntry, the right fix is to also remove the asymmetric removeListener fallback from queryUnsubscribe. Now both sides consistently use the standard addEventListener/removeEventListener API.

@xobotyi would you mind taking a look? It's a small cleanup — just removing 4 lines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants