use-media
useMedia
React sensor hook that tracks state of a CSS media query.
Usage
With useEffect
import {useMedia} from 'use-media';
const Demo = () => {
const isWide = useMedia({ minWidth: 1000 });
const reduceMotion = useMedia('(prefers-reduced-motion: reduce)');
return (
<div>
Screen is wide: {isWide ? '😃' : '😢'}
</div>
);
};
With useLayoutEffect
import {useMediaLayout} from 'use-media';
const Demo = () => {
const isWide = useMediaLayout({ minWidth: 1000 });
const reduceMotion = useMediaLayout('(prefers-reduced-motion: reduce)');
return (
<div>
Screen is wide: {isWide ? '😃' : '😢'}
</div>
);
};