Documentation Index
Fetch the complete documentation index at: https://mintlify.com/mui/base-ui/llms.txt
Use this file to discover all available pages before exploring further.
useMediaQuery
A React hook for detecting media query matches using the browser’swindow.matchMedia API with support for SSR.
This utility is currently unstable and may change in future releases.
Import
Hook Signature
Parameters
The media query string to match. The
@media prefix is optional and will be automatically removed if present.Example: "(min-width: 768px)" or "@media (min-width: 768px)"Configuration options for the media query matching behavior.
Options
As
window.matchMedia() is unavailable on the server, it returns a default matches during the first mount.You can provide your own implementation of matchMedia. This can be used for handling an iframe content window.
To perform the server-side hydration, the hook needs to render twice. A first time with
defaultMatches, the value of the server, and a second time with the resolved value. This double pass rendering cycle comes with a drawback: it’s slower. You can set this option to true if you use the returned value only client-side.You can provide your own implementation of
matchMedia, it’s used when rendering server-side.Return Value
Returnstrue if the media query matches, false otherwise.
Type Definitions
useMediaQuery.Options
Usage Examples
Basic Usage
SSR with Custom Matching
Client-Side Only
SSR Considerations
Sincewindow.matchMedia() is not available on the server:
- Default behavior: Returns
defaultMatcheson first render - With
ssrMatchMedia: Uses custom server-side matching logic - With
noSsr: true: Skips server-side rendering and only evaluates client-side
useSyncExternalStore internally to ensure proper hydration and reactivity to media query changes.
Browser Support
This hook relies onwindow.matchMedia which is supported in all modern browsers. For jsdom environments (like test runners), a defensive check ensures the hook doesn’t crash when matchMedia is unavailable.