# react-mq

> Media Query Component for React

Latest version **0.1.0** (published 2015-02-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-mq
pnpm add react-mq
yarn add react-mq
bun add react-mq
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2015-02-17 |
| First published | 2015-02-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 26 |
| Author | Yuanyan Cao |
| Maintainers | yuanyan |
| Keywords | react, react-component, media, media query |

## Links

- npm: https://www.npmjs.com/package/react-mq
- Repository: https://github.com/yuanyan/react-mq
- Issues: https://github.com/yuanyan/react-mq/issues
- npm.io page: https://npm.io/package/react-mq

## Dependencies (2)

- [react](https://npm.io/package/react.md) ^0.12.0
- [reactify](https://npm.io/package/reactify.md) ^0.17.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.1.0 (latest) — 2015-02-17
- 0.0.0 — 2015-02-14

## README

React Media Query
=================

Media Query Component for React.

## Demo & Examples

Live demo: [yuanyan.github.io/react-mq](http://yuanyan.github.io/react-mq/)

To build the examples locally, run:

```
npm install
gulp dev
```

Then open [`localhost:9999`](http://localhost:9999) in a browser.

## Installation

The easiest way to use `react-mq` is to install it from NPM and include it in your own React build process (using [Browserify](http://browserify.org), etc).

You can also use the standalone build by including `dist/react-mq.js` in your page. If you use this, make sure you have already included React, and it is available as a global variable.

```
npm install react-mq --save
```

## Usage

### Media Query
```
var React = require('react');
var Media = require('react-mq');
var App = React.createClass({
  render: function(){
    return (
      <div>
        <Media query='(max-width: 768px)'>
          <div>You are mobile screen</div>
        </Media>
        <Media query='(min-width: 769px) and (max-width: 992px)'>
          <div>You are tablet screen</div>
        </Media>
        <Media query='(min-width: 993px) and (max-width: 1200px)'>
          <div>You are desktop screen</div>
        </Media>
        <Media query='(min-width: 1201px)'>
          <div>You are jumbo screen</div>
        </Media>
      </div>
    );
  }
});
```

### Nested Media Query
```
var React = require('react');
var Media = require('react-mq');
var App = React.createClass({
  render: function(){
    return (
      <div>
        <Media query='(max-width: 768px)'>
          <div>You are a mobile screen</div>
          <Media query='(orientation: portrait)'>
            <div>You are portrait</div>
          </Media>
          <Media query='(orientation: landscape)'>
            <div>You are landscape</div>
          </Media>
          <Media query='(min-resolution: 2dppx)'>
            <div>You are retina</div>
          </Media>
          <Media query='(max-resolution: 1dppx)'>
            <div>You are not retina</div>
          </Media>
        </Media>
      </div>
    );
  }
});
```

### Mixin

```
var React = require('react');
var MediaMixin = require('react-mq').Mixin;
// Global scope
MediaMixin.addMediaQueries({
  mobile:  '(max-width: 768px)',
  tablet:  '(min-width: 769px) and (max-width: 992px)',
  desktop: '(min-width: 993px) and (max-width: 1200px)'
});

var App = React.createClass({
  mixins: [MediaMixin],
  // Component scope 
  media: {
      jumbo:  '(min-width: 1201px)'
  },
  render: function(){
        // State will update whenever a media query matches or unmatches
        if (this.state.media.mobile){
            return <div>You are mobile screen</div>
        }
        else if (this.state.media.tablet) {
            return <div>You are tablet screen</div>
        }
        else if (this.state.media.desktop) {
            return <div>You are desktop screen</div>
        }
        else {
            return <div>You are jumbo screen</div>
        }
  }
});
```

---
_Source: https://npm.io/package/react-mq · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
