Skip to content

Commit

Permalink
fix(use-media-query): hook doesn't observe viewport changes (#1853)
Browse files Browse the repository at this point in the history
  • Loading branch information
mg901 authored Apr 12, 2024
1 parent 8b6ab85 commit 242e849
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions use-media-query/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ exports.useMediaQuery = useMediaQuery;
* @returns {boolean} - `true` if the media query matches, otherwise `false`.
*/
function useMediaQuery(query) {
const [state, setState] = useState(IS_BROWSER && getInitialState(query));
const [isMatch, setIsMatch] = useState(IS_BROWSER && getInitialState(query));

useEnhancedEffect(() => {
let mounted = true;
Expand All @@ -26,10 +26,12 @@ function useMediaQuery(query) {
if (!mounted) return;

/* istanbul ignore next */
setState(mediaQueryList.matches);
setIsMatch(mediaQueryList.matches);
};

setState(mediaQueryList.matches);
mediaQueryList.addEventListener('change', handleChange);

setIsMatch(mediaQueryList.matches);

return () => {
mounted = false;
Expand All @@ -38,7 +40,7 @@ function useMediaQuery(query) {
};
}, [query]);

return state;
return isMatch;
}

function getInitialState(query) {
Expand Down

0 comments on commit 242e849

Please sign in to comment.