useMediaQuery
Subscribe to a CSS media query, SSR-safe.
useMediaQuery tells you whether a CSS media query currently matches and
re-renders when that changes. It's SSR-safe and works in any React environment —
Vite, CRA, Next.js, Remix, Astro.
Usage
A common pattern: render a Dialog on desktop and a Drawer on mobile.
Signature
On the server and during the first client render window.matchMedia isn't available, so the hook returns
defaultValue. The real value resolves on hydration via useSyncExternalStore. Set defaultValue to your most
likely case to minimize layout shift.