Accepts a media query string then uses the matchMedia API to determine if it matches with the current document.
It also monitor the document changes to detect when it stops matching the given media query.
Returns the validity state of the given media query.
- takes care of re-rendering the component when the given media query changes
- get rid of the listener when the component will unmount
import { useMediaQuery } from 'beautiful-react-hooks';
const MediaQueryReporter = () => {
const isSmall = useMediaQuery('(max-width: 48rem)');
const isLarge = useMediaQuery('(min-width: 48rem)');
return (
<DisplayDemo>
<p>Small view? {isSmall ? 'yes' : 'no'}</p>
<p>Large view? {isLarge ? 'yes' : 'no'}</p>
</DisplayDemo>
);
};
<MediaQueryReporter />
- When a component should have a different layout/behaviour on different medias
- Mount/Unmount sub-components according to a defined media-query
- Do not use this hook to define the user device