Current viewport
Desktop
Tablet
Mobile
Try resizing your browser window to see the changes
0 × 0
useMediaQuery
A React hook that tracks the state of a CSS media query, allowing for responsive logic in your components.
- Create responsive components that adapt to changing viewport sizes
- Supports all standard CSS media queries (width, orientation, dark mode, etc.)
- Efficient updates on window resize or device orientation changes
Installation
The useMediaQuery hook is part of our UI library. You can import it directly from the hooks directory.
Usage
Here is how to use the useMediaQuery hook in your project:
React TSX
React TSX
API Reference
React TSX
Inputs
query
: A CSS media query string to evaluate
Output
matches
: A boolean representing whether the media query matches or not
Behavior
- Hook evaluates the provided media query using window.matchMedia
- Returns a boolean indicating whether the query matches
- Re-evaluates when the window size changes or orientation changes
- Cleans up event listeners on component unmount
Implementation
React TSX