Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
R

react-media

> 编程语言
Open source

CSS media queries for React

2.4K stars0 likes2 views
WebsiteGitHub

About

CSS media queries for React

react-media Travis npm package

react-media is a CSS media query component for React.

A <Media> component listens for matches to a CSS media query and renders stuff based on whether the query matches or not.

Installation

Using npm:

$ npm install --save react-media

Then, use as you would anything else:

javascript
// using ES modules
import Media from 'react-media';

// using CommonJS modules
var Media = require('react-media');

The UMD build is also available on unpkg:

xml
<script src="https://unpkg.com/react-media"></script>

You can find the library on window.ReactMedia.

Hooks are coming soon!

Hooks are available in 2.X branch.

Install react-media@next to get it.

useMedia accepts a single options argument to handle both single and multiple queries, so the same properties as Media are available (except of course render and children props).

Simple usage with multiple queries:

typescript
import { useMedia } from 'react-media';

const GLOBAL_MEDIA_QUERIES = {
    small: "(max-width: 599px)",
    medium: "(min-width: 600px) and (max-width: 1199px)",
    large: "(min-width: 1200px)"
};
const matches = useMedia({ queries: GLOBAL_MEDIA_QUERIES });

const marginBottom = matches.large ? 0 : 10;

With single query :

typescript
import { useMedia } from 'react-media';

const isSmallScreen = useMedia({ query: "(max-width: 599px)" });

Basic usage

queries

Render a <Media> component with a queries prop whose value is an object, where each value is a valid CSS media query. The children prop should be a function whose argument will be an object with the same keys as your queries object, and whose values are booleans indicating whether each query matches.

javascript
import React, { Fragment } from 'react';
import Media from 'react-media';

class App extends React.Component {
  render() {
    return (
      

    );
  }
}

query

Alternatively, if you only need to match against a single media query, the query prop provides a less-verbose approach. More documentation about the difference between query and queries can be found below.

javascript
import React, { Fragment } from 'react';
import Media from 'react-media';

class App extends React.Component {
  render() {
    return (
      

    );
  }
}

query vs queries

The queries prop was added to allow for multiple media queries to be matched without excessive nesting or other workarounds. The query prop was retained out of recognition that a single query covers many use cases, and there is already a lot of usage that would be a pain to migrate.

The salient points:

  • You cannot use them together: if you do, the component will throw an error. This is to avoid confusion around precedence.
  • The render methods differ slightly: for the queries prop, the render and child JSX methods will render if at least one of the given queries is matched. The query prop renders if the given query matches.

queries

In addition to passing a valid media query string, the queries prop will also accept an object of objects whose forms are similar to React's built-in support for inline style objects in e.g. `

);

} }

…

Keys of media query objects are camel-cased and numeric values automatically get the px suffix. See the json2mq docs for more examples of queries you can construct using objects.

Render props

There are three props which allow you to render your content. They each serve a subtly different purpose.

prop description example
render Only invoked when the query matches. This is a nice shorthand if you only want to render something for a matching query. <Media query="..." render={() => <p>I matched!</p>} />
children (function) Receives a single boolean element, indicating whether the media query matched. Use this prop if you need to render something when the query doesn't match. <Media query="...">{matches => matches ? <p>I matched!</p> : <p>I didn't match</p>}</Media>
children (react element) If you render a regular React element within <Media>, it will render that element when the query matches. This method serves the same purpose as the render prop, however, you'll create component instances regardless of whether the query matches or not. Hence, using the render prop is preferred (more info). <Media query="..."><p>I matched!</p></Media>

onChange

You can specify an optional onChange prop, which is a callback function that will be invoked when the status of the media queries changes. This can be useful for triggering side effects, independent of the render lifecycle.

javascript
import React from 'react';
import Media from 'react-media';

class App extends React.Component {
  render() {
    return (
      

    );
  }
}

Server-side rendering (SSR)

If you render a <Media> component on the server, it will match by default. You can override the default behavior by setting the defaultMatches prop.

When rendering on the server you can use the defaultMatches prop to set the initial state on the server to match whatever you think it will be on the client. You can detect the user's device by analyzing the user-agent string from the HTTP request in your server-side rendering code.

javascript
initialState = {
  device: 'mobile' // add your own guessing logic here, based on user-agent for example
};

;

targetWindow

An optional targetWindow prop can be specified if you want the queries to be evaluated against a different window object than the one the code is running in. This can be useful if you are rendering part of your component tree to an iframe or a popup window. See this PR thread for context.

About

react-media is developed and maintained by React Training. If you're interested in learning more about what React can do for your company, please get in touch!

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

JavaScript

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言