explaingit

yocontra/react-responsive

7,171TypeScriptAudience · developerComplexity · 2/5Setup · easy

TLDR

A React library that lets you write responsive layout rules directly inside your components. Check screen size, orientation, or pixel density in JavaScript and show or hide content based on the result.

Mindmap

mindmap
  root((react-responsive))
    Usage
      useMediaQuery hook
      MediaQuery component
    Conditions
      Screen width
      Orientation
      Pixel density
    SSR support
      Fake device config
      React context
    Features
      onChange callback
      Reusable breakpoints
      Next.js guidance
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Show a mobile navigation menu on small screens and a sidebar on wide screens using a simple hook inside your React component.

USE CASE 2

Fix server-side rendering issues where media queries fail because there is no browser, pass fake device settings to get correct output.

USE CASE 3

Run a function every time the user resizes their browser past a breakpoint using the onChange callback.

Tech stack

TypeScriptReact

Getting it running

Difficulty · easy Time to first run · 5min
License is not mentioned in the documentation.

In plain English

react-responsive is a TypeScript library for React that lets you apply CSS media queries directly inside your components. Instead of writing responsive rules in a stylesheet, you describe conditions like screen width or orientation in JavaScript and React handles showing or hiding content based on whether those conditions are met on the current device. There are two ways to use it. The first is a hook called useMediaQuery, which you call inside a function component. You pass in a query object describing the screen condition you want to check, and it returns true or false. The second is a component called MediaQuery that wraps content and only renders its children when the query matches. Both approaches support the same set of conditions: minimum and maximum widths, screen orientation, pixel density, and more. A useful feature for server-side rendering and testing is the ability to force a fake device configuration. When a page renders on a server, there is no real browser to detect screen size, which would normally cause the queries to fail or produce incorrect output. By passing in a device object with fixed values, you can tell the library to treat the environment as if it were a specific screen size. This can be provided per component or through a React context that applies to an entire component tree. The library also supports an onChange callback, so you can run a function whenever a media query result changes, for example when a user resizes their browser window. This makes it straightforward to trigger side effects based on layout changes. Installation is via npm. The README includes working code examples for all major use cases, including a pattern for defining reusable breakpoint components for desktop, tablet, and mobile, as well as Next.js-specific guidance for disabling server-side rendering on components that use the library.

Copy-paste prompts

Prompt 1
I'm using react-responsive's useMediaQuery hook and want to show a different layout on mobile vs desktop. Show me the code to check if the screen is wider than 768px.
Prompt 2
My Next.js app shows the wrong content on first load because react-responsive doesn't know the screen size on the server. How do I fix this using the device prop?
Prompt 3
I want to define reusable breakpoints for mobile, tablet, and desktop using react-responsive so I don't repeat query strings everywhere. How do I set that up?
Prompt 4
I want to trigger a side effect whenever the user switches between portrait and landscape orientation. How do I use the onChange callback for that?
Prompt 5
How do I share the same device config across my whole React app using react-responsive's context instead of passing it to every component?
Open on GitHub → Explain another repo

← yocontra on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.