React Responsive Media Queries
react-responsive is a well-established and actively maintained library (current stable version 10.0.1) that simplifies the integration of CSS media queries into React applications. It provides a declarative API for responsive design, allowing developers to render components or apply logic based on viewport characteristics. The library offers two primary interfaces: the modern `useMediaQuery` hook (introduced in v8.0.0) for functional components and a `MediaQuery` component (using render props) for class components or older patterns. Key differentiators include its support for both hooks and component-based approaches, convenient camel-cased shorthand properties for media query expressions, and a `device` prop for explicitly setting device characteristics, which is crucial for server-side rendering (SSR) and testing environments. It also ships with comprehensive TypeScript types, ensuring robust development. The library maintains a steady release cadence, focusing on stability and modern React paradigms.
Common errors
-
TypeError: (0 , react_responsive__WEBPACK_IMPORTED_MODULE_2__.useMediaQuery) is not a function
cause Attempting to use `useMediaQuery` in a project where `react-responsive` version is older than 8.0.0.fixUpgrade `react-responsive` to version `8.0.0` or higher (`npm install react-responsive@latest` or `yarn add react-responsive@latest`). -
ReferenceError: window is not defined
cause The component using `react-responsive` is being rendered in a Node.js environment (e.g., SSR) without providing a `device` prop to simulate `window.matchMedia`.fixWhen using `useMediaQuery` or `MediaQuery` in SSR, provide a second argument with a `device` object, e.g., `useMediaQuery({ minWidth: 1024 }, { deviceWidth: 1920, deviceHeight: 1080, type: 'screen' })`. -
TypeError: Cannot read properties of undefined (reading 'addListener') at MatchMedia.componentDidMount
cause This often occurs in testing environments (like Jest) where `window.matchMedia` is not mocked, and `react-responsive` attempts to call its methods.fixMock `window.matchMedia` in your test setup. A common mock is `window.matchMedia = jest.fn().mockImplementation(query => ({ matches: false, media: query, onchange: null, addListener: jest.fn(), removeListener: jest.fn(), dispatchEvent: jest.fn() }));`.
Warnings
- breaking The `useMediaQuery` hook was introduced in version 8.0.0. Projects using older versions of `react-responsive` will not have access to this API and must use the `MediaQuery` component or upgrade.
- gotcha When using `react-responsive` in a Node.js environment (e.g., Server-Side Rendering - SSR), `window.matchMedia` is unavailable, leading to incorrect or no media query matches. You must explicitly provide device characteristics via the `device` prop.
- gotcha The `device` prop, when provided, always takes precedence over actual client-side `window.matchMedia` detections. This can lead to unexpected behavior if not understood, as the component will render based on the provided `device` values, even if the user's actual device settings differ.
- gotcha Shorthand properties like `minWidth` or `maxWidth` automatically append `px` to numeric values. If you intend to use other units (e.g., `em`, `rem`), you must provide the value as a string.
Install
-
npm install react-responsive -
yarn add react-responsive -
pnpm add react-responsive
Imports
- useMediaQuery
const { useMediaQuery } = require('react-responsive')import { useMediaQuery } from 'react-responsive' - MediaQuery
import { MediaQuery } from 'react-responsive'import MediaQuery from 'react-responsive'
- UseMediaQueryOptions
import { UseMediaQueryOptions } from 'react-responsive'import type { UseMediaQueryOptions } from 'react-responsive'
Quickstart
import React from 'react';
import { useMediaQuery } from 'react-responsive';
const MyResponsiveComponent = () => {
const isMobile = useMediaQuery({ maxWidth: 767 });
const isTablet = useMediaQuery({ minWidth: 768, maxWidth: 1023 });
const isDesktop = useMediaQuery({ minWidth: 1024 });
const isPortrait = useMediaQuery({ orientation: 'portrait' });
return (
<div>
<h2>Responsive View</h2>
{isMobile && <p>You are viewing this on a Mobile device!</p>}
{isTablet && <p>You are viewing this on a Tablet!</p>}
{isDesktop && <p>You are viewing this on a Desktop!</p>}
<p>Orientation: {isPortrait ? 'Portrait' : 'Landscape'}</p>
<p>This demonstrates how to use the `useMediaQuery` hook to conditionally render content based on various screen dimensions and orientations. It's a clean way to apply responsive logic directly within your functional components.</p>
</div>
);
};
export default MyResponsiveComponent;