Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-media

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-media

CSS media queries for React

  • 1.10.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created

What is react-media?

The react-media package is a versatile tool for handling CSS media queries in React applications. It allows developers to conditionally render components based on the current viewport size or other media query conditions, making it easier to create responsive designs.

What are react-media's main functionalities?

Basic Media Query

This feature allows you to render different components based on the viewport width. In this example, it displays 'Small Screen' if the viewport width is 599px or less, and 'Large Screen' otherwise.

```jsx
import React from 'react';
import Media from 'react-media';

const App = () => (
  <Media query="(max-width: 599px)">
    {matches => matches ? <p>Small Screen</p> : <p>Large Screen</p>}
  </Media>
);

export default App;
```

Multiple Media Queries

This feature allows you to handle multiple media queries at once. The example demonstrates how to render different components for small, medium, and large screens based on the viewport width.

```jsx
import React from 'react';
import Media from 'react-media';

const App = () => (
  <Media queries={{ small: "(max-width: 599px)", medium: "(min-width: 600px) and (max-width: 1199px)", large: "(min-width: 1200px)" }}>
    {matches => (
      <div>
        {matches.small && <p>Small Screen</p>}
        {matches.medium && <p>Medium Screen</p>}
        {matches.large && <p>Large Screen</p>}
      </div>
    )}
  </Media>
);

export default App;
```

Server-Side Rendering

This feature supports server-side rendering by providing a `defaultMatches` prop. This ensures that the component renders correctly on the server before the client-side JavaScript takes over.

```jsx
import React from 'react';
import Media from 'react-media';

const App = () => (
  <Media query="(max-width: 599px)" defaultMatches={true}>
    {matches => matches ? <p>Small Screen</p> : <p>Large Screen</p>}
  </Media>
);

export default App;
```

Other packages similar to react-media

Keywords

FAQs

Package last updated on 16 Sep 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc