Playground

useMediaQuery

Whether a CSS media query matches, updated live, and safe to render on the server.

  • No DOM output. The first render returns defaultValue so server and client markup match.
  • Listens to matchMedia changes and removes the listener on unmount.

Import

import '@monoline/css';
import {useMediaQuery} from '@monoline/react';

Not installed yet? Pick a route.

Copies the source and its styles into your project.

npx monoline add use-media-query

Usage

import {useMediaQuery} from '@monoline/react';

function WidthDemo() {
  const wide = useMediaQuery('(min-width: 40rem)');
  return <p>{wide ? 'Viewport is 40rem or wider.' : 'Viewport is narrower than 40rem.'}</p>;
}

<WidthDemo />

Examples

Viewport width

Resize the window to see it change.

Behaviour

  • No DOM output. The first render returns defaultValue so server and client markup match.
  • Listens to matchMedia changes and removes the listener on unmount.