Join our Discord Community

React useMediaQuery Hook

React useMediaQuery hook for responsive design. Track breakpoints and user preferences with real-time updates and SSR support using TypeScript for Next.js.

Responsive Breakpoints Acting Up?

Join our Discord community for help from other developers.


Ever tried to build responsive components in React and ended up with window.innerWidth checks, resize event listeners, and broken SSR? You know the drill—manually tracking screen sizes, dealing with hydration mismatches, forgetting to clean up event listeners. This free open source React useMediaQuery custom hook handles all that responsive complexity so you can focus on building adaptive UIs instead of reinventing media query logic in your React applications.

useMediaQuery showcase

Real-time responsive design with automatic breakpoint tracking:

Loading component...

This free open source React hook simplifies responsive design with TypeScript support for modern JavaScript applications. Whether you're building mobile-first layouts, dark mode detection, or accessibility features in your Next.js projects, this React hook keeps your media queries reactive.

Installation

npx shadcn@latest add https://www.shadcn.io/registry/use-media-query.json
npx shadcn@latest add https://www.shadcn.io/registry/use-media-query.json
pnpm dlx shadcn@latest add https://www.shadcn.io/registry/use-media-query.json
bunx shadcn@latest add https://www.shadcn.io/registry/use-media-query.json

Why most media query implementations suck

Look, you could keep writing window.innerWidth checks and resize event listeners. But then you hit the responsive web complexity—server-side rendering issues, event cleanup headaches, inconsistent breakpoint logic across components in React applications.

Most developers manually track window dimensions with resize listeners that fire constantly, causing performance issues in TypeScript components. Or they create custom breakpoint logic that doesn't match CSS media queries, leading to inconsistent responsive behavior. Some forget about event cleanup and wonder why their resize handlers accumulate over time in Next.js projects.

This React hook uses the native matchMedia API under the hood, which is specifically designed for efficient media query tracking in JavaScript applications. The browser handles all the optimization, plus you get real-time updates when queries change.

Plus it handles all the edge cases—SSR compatibility, automatic event cleanup, Safari fallbacks for older versions in React development. No more scattered resize listeners or hydration errors.

This free open source React hook manages responsive state while you focus on building features. Whether you're creating React applications, Next.js sites, or TypeScript components, reliable media query detection keeps your JavaScript development adaptive.

Features

  • Responsive design with automatic breakpoint tracking and screen size detection in React applications
  • User preferences detect dark mode, reduced motion, and accessibility settings in TypeScript components
  • Real-time updates automatically updates when media query state changes in Next.js projects
  • SSR compatible with optional initialization for server-side rendering in JavaScript development
  • Safari support uses deprecated listeners for Safari < 14 compatibility in React frameworks
  • Type safety with full TypeScript support and proper return types for modern applications
  • Free open source designed for modern React development workflows

When you'll actually use this

Real talk—this isn't for every responsive need in React applications. CSS media queries handle most styling cases perfectly. But when you need responsive logic in your React components or JavaScript behavior, this React hook delivers in Next.js projects.

Perfect for:

  • Responsive components - Show/hide components based on screen size built with TypeScript
  • User preferences - Detect dark mode, reduced motion, or high contrast settings using React patterns
  • Navigation patterns - Mobile hamburger menus vs desktop navigation bars in JavaScript applications
  • Conditional rendering - Different layouts for mobile vs desktop experiences in React components
  • Accessibility features - Adapt behavior based on user motion preferences in Next.js applications
  • Device detection - Portrait/landscape orientation and touch capability using TypeScript safety

API Reference

useMediaQuery

useMediaQuery(query: string, options?: UseMediaQueryOptions): boolean
ParameterTypeDescription
querystringThe media query string to track
optionsUseMediaQueryOptionsOptional configuration for the hook

UseMediaQueryOptions

PropertyTypeDefaultDescription
defaultValuebooleanfalseDefault value returned on server or before initialization
initializeWithValuebooleantrueWhether to initialize with actual media query state (set to false for SSR)

Return Value

Returns a boolean indicating whether the media query currently matches.

Common gotchas

SSR requires careful initialization: Use initializeWithValue: false for SSR to prevent hydration mismatches in React applications. The React hook will start with your default value and update after client hydration.

Media queries are case-sensitive: Make sure your query syntax is correct in TypeScript components. Invalid queries will always return false without throwing errors.

Performance is generally excellent: The matchMedia API is optimized by browsers in Next.js projects, but avoid creating too many concurrent media query listeners for complex responsive logic.

Mobile browsers handle orientation changes: Orientation queries update automatically in JavaScript applications, but there might be a small delay during device rotation.

Browser compatibility varies: Some media query features aren't supported in older browsers in React frameworks. Always test your responsive logic across different devices.

Timing considerations: Media query changes are asynchronous and may not update immediately in TypeScript projects. Handle loading states appropriately for better user experience.

Questions you might have