useMediaQuery
the useMediaQuery
module provides hooks for working with responsive breakpoints in React applications. its written using the window.matchMedia
API and facade
pattern.
this module provides 3 hooks:
-
useMediaQuery
: you can pass any css query string to this hook, it also supports auto complete.const isLg = useMediaQuery("(min-width: 64rem)"); const isNotMd = useMediaQuery("(max-width: 48rem)");
-
useMediaQueryBreakpoint
: based on Tailwind breakpoints, by default similar to tailwindcss it uses mobile first approach and usesmin-width
you can swap the direction tomax
to check for the breakpoint and belowconst isMobile = useMediaQueryBreakpoint("sm"); const isTablet = useMediaQueryBreakpoint("md", "max"); const isDesktop = useMediaQueryBreakpoint("lg", "min");
-
useIsMobile
: returns true onsm
/640px
/40rem
and belowconst isMobile = useIsMobile();