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
Copy file name to clipboardExpand all lines: README.md
+56-7Lines changed: 56 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -35,6 +35,8 @@ You can find the library on `window.ReactMedia`.
35
35
36
36
## Basic usage
37
37
38
+
### queries
39
+
38
40
Render a `<Media>` component with a `queries` prop whose value is an object,
39
41
where each value is a valid
40
42
[CSS media query](https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries).
@@ -69,15 +71,42 @@ class App extends React.Component {
69
71
}
70
72
```
71
73
72
-
##Render props
74
+
### query
73
75
74
-
There are three props which allow you to render your content. They each serve a subtly different purpose.
76
+
Alternatively, if you only need to match against a single media query, the `query` prop provides a less-verbose approach.
77
+
More documentation about the difference betwen `query` and `queries` can be found below.
75
78
76
-
|prop|description|example|
77
-
|---|---|---|
78
-
|render|Only invoked when **at least one** of the queries matches. This is a nice shorthand if you only want to render something for a matching query.|`<Media query={{ foo: ... }} render={() => <p>I matched!</p>} />`|
79
-
|children (function)|Receives an object of booleans whose keys are the same as the `queries` prop, indicating whether each media query matched. Use this prop if you need to render different output for each of specified queries.|`<Media queries={{ foo: ... }}>{matches => matches.foo ? <p>I matched!</p> : <p>I didn't match</p>}</Media>`|
80
-
|children (react element)|If you render a regular React element within `<Media>`, it will render that element when **at least one** of the queries matches. This method serves the same purpose as the `render` prop, however, you'll create component instances regardless of whether the queries match or not. Hence, using the `render` prop is preferred ([more info](https://github.com/ReactTraining/react-media/issues/70#issuecomment-347774260)).|`<Media queries={{ ... }}><p>I matched!</p></Media>`|
79
+
```jsx
80
+
importReact, { Fragment } from'react';
81
+
importMediafrom'react-media';
82
+
83
+
classAppextendsReact.Component {
84
+
render() {
85
+
return (
86
+
<div>
87
+
<Media query="(max-width: 599px)" render={() =>
88
+
(
89
+
<p>I am small!</p>
90
+
)}
91
+
/>
92
+
</div>
93
+
);
94
+
}
95
+
}
96
+
```
97
+
98
+
## `query` vs `queries`
99
+
100
+
The `queries` prop was added to allow for multiple media queries to be matched without excessive nesting or other
101
+
workarounds. The `query` prop was retained out of recognition that a single query covers many use cases, and there
102
+
is already a lot of usage that would be a pain to migrate.
103
+
104
+
The salient points:
105
+
106
+
***You cannot use them together**: if you do, the component will throw an error. This is to avoid confusion around
107
+
precedence.
108
+
***The render methods differ slightly**: for the `queries` prop, the `render` and child JSX methods will render if
109
+
**at least one** of the given queries is matched. The `query` prop renders if the given query matches.
81
110
82
111
83
112
## `queries`
@@ -125,6 +154,16 @@ class App extends React.Component {
125
154
126
155
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.
127
156
157
+
### Render props
158
+
159
+
There are three props which allow you to render your content. They each serve a subtly different purpose.
160
+
161
+
|prop|description|example|
162
+
|---|---|---|
163
+
|render|Only invoked when **at least one** of the queries matches. This is a nice shorthand if you only want to render something for a matching query.|`<Media queries={{ foo: ... }} render={() => <p>I matched!</p>} />`|
164
+
|children (function)|Receives an object of booleans whose keys are the same as the `queries` prop, indicating whether each media query matched. Use this prop if you need to render different output for each of specified queries.|`<Media queries={{ foo: ... }}>{matches => matches.foo ? <p>I matched!</p> : <p>I didn't match</p>}</Media>`|
165
+
|children (react element)|If you render a regular React element within `<Media>`, it will render that element when **at least one** of the queries matches. This method serves the same purpose as the `render` prop, however, you'll create component instances regardless of whether the queries match or not. Hence, using the `render` prop is preferred ([more info](https://github.com/ReactTraining/react-media/issues/70#issuecomment-347774260)).|`<Media queries={{ ... }}><p>I matched!</p></Media>`|
166
+
128
167
## `query`
129
168
130
169
In addition to passing a valid media query string, the `query` prop will also accept 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).
@@ -166,6 +205,16 @@ class App extends React.Component {
166
205
167
206
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.
168
207
208
+
### Render props
209
+
210
+
There are three props which allow you to render your content. They each serve a subtly different purpose.
211
+
212
+
|prop|description|example|
213
+
|---|---|---|
214
+
|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>} />`|
215
+
|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>`|
216
+
|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](https://github.com/ReactTraining/react-media/issues/70#issuecomment-347774260)).|`<Media query="..."><p>I matched!</p></Media>`|
217
+
169
218
## `onChange`
170
219
171
220
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.
0 commit comments