Skip to content

Commit a832017

Browse files
committed
Add alternative usage in getting started, add section on differences, reinstate renderprops explanation for queyr
1 parent 557aaad commit a832017

File tree

1 file changed

+56
-7
lines changed

1 file changed

+56
-7
lines changed

README.md

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ You can find the library on `window.ReactMedia`.
3535

3636
## Basic usage
3737

38+
### queries
39+
3840
Render a `<Media>` component with a `queries` prop whose value is an object,
3941
where each value is a valid
4042
[CSS media query](https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries).
@@ -69,15 +71,42 @@ class App extends React.Component {
6971
}
7072
```
7173

72-
## Render props
74+
### query
7375

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.
7578

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+
import React, { Fragment } from 'react';
81+
import Media from 'react-media';
82+
83+
class App extends React.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.
81110

82111

83112
## `queries`
@@ -125,6 +154,16 @@ class App extends React.Component {
125154

126155
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.
127156

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+
128167
## `query`
129168

130169
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 {
166205

167206
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.
168207

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+
169218
## `onChange`
170219

171220
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

Comments
 (0)