You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[`react-media`](https://www.npmjs.com/package/react-media) is a CSS media query component for React.
10
9
11
-
A `<Media>` component listens for matches to a [CSS media query](https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries) and renders stuff based on whether the query matches or not.
10
+
A `<Media>` component listens for matches to a
11
+
[CSS media query](https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries) and renders stuff
12
+
based on whether the query matches or not.
12
13
13
14
## Installation
14
15
15
16
Using [npm](https://www.npmjs.com/):
16
17
17
18
$ npm install --save react-media
18
19
19
-
Then with a module bundler like [webpack](https://webpack.github.io/), use as you would anything else:
20
+
Then with a module bundler like [webpack](https://webpack.github.io/), use as you would anything
21
+
else:
20
22
21
23
```js
22
24
// using ES modules
23
-
importMediafrom'react-media'
25
+
importMediafrom"react-media"
24
26
25
27
// using CommonJS modules
26
-
var Media =require('react-media')
28
+
var Media =require("react-media")
27
29
```
28
30
29
31
The UMD build is also available on [unpkg](https://unpkg.com):
@@ -36,22 +38,27 @@ You can find the library on `window.ReactMedia`.
36
38
37
39
## Usage
38
40
39
-
Render a `<Media>` component with a `query` prop whose value is a valid [CSS media query](https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries). The `children` prop should be a function whose only argument will be a boolean flag that indicates whether the media query matches or not.
41
+
Render a `<Media>` component with a `query` prop whose value is a valid
42
+
[CSS media query](https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries). The `children`
43
+
prop should be a function whose only argument will be a boolean flag that indicates whether the
44
+
media query matches or not.
40
45
41
46
```jsx
42
-
importReactfrom'react'
43
-
importMediafrom'react-media'
47
+
importReactfrom"react"
48
+
importMediafrom"react-media"
44
49
45
50
classAppextendsReact.Component {
46
51
render() {
47
52
return (
48
53
<div>
49
54
<Media query="(max-width: 599px)">
50
-
{matches=> matches ? (
51
-
<p>The document is less than 600px wide.</p>
52
-
) : (
53
-
<p>The document is at least 600px wide.</p>
54
-
)}
55
+
{matches=>
56
+
matches ? (
57
+
<p>The document is less than 600px wide.</p>
58
+
) : (
59
+
<p>The document is at least 600px wide.</p>
60
+
)
61
+
}
55
62
</Media>
56
63
</div>
57
64
)
@@ -61,21 +68,27 @@ class App extends React.Component {
61
68
62
69
If you render a `<Media>` component on the server, it always matches.
63
70
64
-
If you use a regular React element as `children` (i.e. `<Media><SomethingHere/></Media>`) it will be rendered if the query matches. However, *you may end up creating a bunch of elements that won't ever actually be rendered to the page* (i.e. you'll do a lot of unnecessary `createElement`s on each `render`). Thus, a `children`**function** (i.e. `<Media>{matches => ...}</Media>`) is the preferred API. Then you can decide in the callback which elements to create based on the result of the query.
71
+
If you use a regular React element as `children` (i.e. `<Media><SomethingHere/></Media>`) it will be
72
+
rendered if the query matches. However, _you may end up creating a bunch of elements that won't ever
73
+
actually be rendered to the page_ (i.e. you'll do a lot of unnecessary `createElement`s on each
74
+
`render`). Thus, a `children`**function** (i.e. `<Media>{matches => ...}</Media>`) is the preferred
75
+
API. Then you can decide in the callback which elements to create based on the result of the query.
65
76
66
-
For the common case of "only render something when the media query matches", you can use a `render` prop that is only called if the query matches.
77
+
For the common case of "only render something when the media query matches", you can use a `render`
78
+
prop that is only called if the query matches.
67
79
68
80
```jsx
69
-
importReactfrom'react'
70
-
importMediafrom'react-media'
81
+
importReactfrom"react"
82
+
importMediafrom"react-media"
71
83
72
84
classAppextendsReact.Component {
73
85
render() {
74
86
return (
75
87
<div>
76
-
<Media query="(max-width: 599px)" render={() => (
77
-
<p>The document is less than 600px wide.</p>
78
-
)}/>
88
+
<Media
89
+
query="(max-width: 599px)"
90
+
render={() =><p>The document is less than 600px wide.</p>}
91
+
/>
79
92
</div>
80
93
)
81
94
}
@@ -84,31 +97,44 @@ class App extends React.Component {
84
97
85
98
The `render` prop is never called if the query does not match.
86
99
87
-
`<Media query>` also accepts an object, similar to [React's built-in support for inline style objects](https://facebook.github.io/react/tips/inline-styles.html) in e.g. `<div style>`. These objects are converted to CSS media queries via [json2mq](https://github.com/akiran/json2mq/blob/master/README.md#usage).
100
+
`<Media query>` also accepts an object, similar to
101
+
[React's built-in support for inline style objects](https://facebook.github.io/react/tips/inline-styles.html)
102
+
in e.g. `<div style>`. These objects are converted to CSS media queries via
Keys of media query objects are camel-cased and numeric values automatically get the `px` suffix. See the [json2mq docs](https://github.com/akiran/json2mq/blob/master/README.md#usage) for more examples of queries you can construct using objects.
128
+
Keys of media query objects are camel-cased and numeric values automatically get the `px` suffix.
129
+
See the [json2mq docs](https://github.com/akiran/json2mq/blob/master/README.md#usage) for more
130
+
examples of queries you can construct using objects.
131
+
132
+
If you're curious about how react-media differs from
133
+
[react-responsive](https://github.com/contra/react-responsive), please see
Development of react-media is done by [React Training](https://reacttraining.com). If you're interested in learning more about what React can do for your company, please [get in touch](mailto:[email protected])!
138
+
Development of react-media is done by [React Training](https://reacttraining.com). If you're
139
+
interested in learning more about what React can do for your company, please
0 commit comments