useMediaQuery
useMediaQuery subscribes to a CSS media query and returns whether it currently matches.
import { useMediaQuery } from '@ilokesto/utilinent/hooks';
function Nav() {
const isDesktop = useMediaQuery('(min-width: 768px)');
return isDesktop ? <DesktopNav /> : <MobileNav />;
}Pass a media query string. The return value updates reactively when the match changes.
SSR and hydration
The server render and the client's first hydration render share the same initial snapshot: the hook reports false. The actual match is applied synchronously in a layout effect before the first paint, so hydrated markup never mismatches.
- The very first client render reports
falseeven when the query already matches; the real value is committed before the browser paints. - When the server and the client media states differ, hydration completes without recoverable hydration errors.
- Changing the
queryre-subscribes to the new query and cleans up the previous change listener. - When
window.matchMediais unavailable, the hook stays onfalsewithout throwing.