Install react-responsive via npm
masterInstall the react-responsive package using npm to use media queries in your React applications.
$ npm install react-responsive --saverepository·master·Indexed 27 days ago
https://github.com/yocontra/react-responsiveA React module for implementing media queries to enable responsive design. It provides the useMediaQuery hook for functional components and the MediaQuery component for conditional rendering. The library supports custom device settings for SSR and testing via ResponsiveContext.Provider, an onChange callback for match changes, and a toQuery utility to convert feature objects into CSS media query strings.
Install the react-responsive package using npm to use media queries in your React applications.
$ npm install react-responsive --saveTo provide device settings to all useMediaQuery hooks in a component tree (e.g., for SSR or unit testing), use ResponsiveContext.Provider from react-responsive.
// Server-Side Rendering example
import { Context as ResponsiveContext } from 'react-responsive'
import { renderToString } from 'react-dom/server'
import App from './App'
const mobileApp = renderToString(
<ResponsiveContext.Provider value={{ width: 500 }}>
<App />
</ResponsiveContext.Provider>
)
// Testing example
import { Context as ResponsiveContext } from 'react-responsive'
import { render } from '@testing-library/react'
import ProductsListing from './ProductsListing'
test('matches the snapshot', () => {
const { container: mobile } = render(
<ResponsiveContext.Provider value={{ width: 300 }}>
<ProductsListing />
</ResponsiveContext.Provider>
)
// ... assertions
})If using Next.js, you can disable server-side rendering for components using react-responsive by using next/dynamic with ssr: false.
import dynamic from 'next/dynamic'
const MediaQuery = dynamic(() => import('react-responsive'), {
ssr: false
})useMediaQuery and MediaQuery support an onChange callback that is triggered whenever the media query match value changes.You can override automatically detected device settings by passing a device object as the second argument to useMediaQuery. This is useful for Server-Side Rendering (SSR) or testing. The device property always takes precedence over detected settings.
import { useMediaQuery } from 'react-responsive'
const Example = () => {
const isDesktopOrLaptop = useMediaQuery(
{ minDeviceWidth: 1224 },
{ deviceWidth: 1600 } // `device` prop overrides detection
)
return (
<div>
{isDesktopOrLaptop && <p>This renders because deviceWidth was overridden to 1600.</p>}
</div>
)
}The useMediaQuery hook (available since v8.0.0) allows you to detect media query matches within a functional component. You can pass a full CSS query string via the query property, or use camel-cased shorthands like minWidth, maxWidth, orientation, and minResolution. Numbers provided as shorthands are automatically converted to px.
import React from 'react'
import { useMediaQuery } from 'react-responsive'
const Example = () => {
const isDesktopOrLaptop = useMediaQuery({ minWidth: 1224 })
const isBigScreen = useMediaQuery({ minWidth: 1824 })
const isTabletOrMobile = useMediaQuery({ maxWidth: 1224 })
const isPortrait = useMediaQuery({ orientation: 'portrait' })
const isRetina = useMediaQuery({ minResolution: '2dppx' })
return (
<div>
{isDesktopOrLaptop && <p>You are a desktop or laptop</p>}
{isBigScreen && <p>You have a huge screen</p>}
{isTabletOrMobile && <p>You are a tablet or mobile phone</p>}
<p>Your are in {isPortrait ? 'portrait' : 'landscape'} orientation</p>
{isRetina && <p>You are retina</p>}
</div>
)
}The MediaQuery component can be used to conditionally render children based on media query matches. It supports two usage patterns:
matches (boolean) as an argument.import MediaQuery from 'react-responsive'
const Example = () => (
<div>
<h1>Device Test!</h1>
<MediaQuery minWidth={1224}>
<p>You are a desktop or laptop</p>
<MediaQuery minWidth={1824}>
<p>You also have a huge screen</p>
</MediaQuery>
</MediaQuery>
<MediaQuery minResolution="2dppx">
{(matches) =>
matches ? <p>You are retina</p> : <p>You are not retina</p>
}
</MediaQuery>
</div>
)When using the device prop to override settings, you can use the following keys:
Keys:
orientationscanaspectRatiodeviceAspectRatioheightdeviceHeightwidthdeviceWidthcolorcolorIndexmonochromeresolutiontypetype values:
all, grid, aural, braille, handheld, print, projection, screen, tty, tv, or embossed.
The library exports several types for defining media queries and features:
MediaQueryTypes: The set of available media query types.MediaQueryType: A specific media query type.MediaQueryFeatures: An object representing CSS media features (e.g., minWidth, orientation).MediaQueryMatchers: Types used for matching queries.MediaQueryAllQueryable: A type for objects that can be queried.MediaQueryAllQueryable interface is the comprehensive type used for defining a complete media query. It combines both MediaQueryFeatures (for dimensions and ranges) and MediaQueryTypes (for the media type).toQuery to convert a MediaQueryFeatures object into a valid CSS media query string.MediaQueryFeatures interface extends MediaQueryMatchers to allow for range-based matching using min and max prefixes for properties like width, height, aspectRatio, resolution, and color.