Skip to content

Commit 93e1f6d

Browse files
committed
Update README
1 parent 89de698 commit 93e1f6d

File tree

1 file changed

+22
-45
lines changed

1 file changed

+22
-45
lines changed

README.md

Lines changed: 22 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,22 @@
77

88
[`react-media`](https://www.npmjs.com/package/react-media) is a CSS media query component for React.
99

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.
10+
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.
1311

1412
## Installation
1513

16-
Using [npm](https://www.npmjs.com/):
14+
Using npm:
1715

1816
$ npm install --save react-media
1917

20-
Then with a module bundler like [webpack](https://webpack.github.io/), use as you would anything
21-
else:
18+
Then, use as you would anything else:
2219

2320
```js
2421
// using ES modules
25-
import Media from "react-media"
22+
import Media from "react-media";
2623

2724
// using CommonJS modules
28-
var Media = require("react-media")
25+
var Media = require("react-media");
2926
```
3027

3128
The UMD build is also available on [unpkg](https://unpkg.com):
@@ -38,14 +35,11 @@ You can find the library on `window.ReactMedia`.
3835

3936
## Usage
4037

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.
38+
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.
4539

4640
```jsx
47-
import React from "react"
48-
import Media from "react-media"
41+
import React from "react";
42+
import Media from "react-media";
4943

5044
class App extends React.Component {
5145
render() {
@@ -61,25 +55,20 @@ class App extends React.Component {
6155
}
6256
</Media>
6357
</div>
64-
)
58+
);
6559
}
6660
}
6761
```
6862

6963
If you render a `<Media>` component on the server, it always matches.
7064

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+
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.
7666

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+
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.
7968

8069
```jsx
81-
import React from "react"
82-
import Media from "react-media"
70+
import React from "react";
71+
import Media from "react-media";
8372

8473
class App extends React.Component {
8574
render() {
@@ -90,21 +79,18 @@ class App extends React.Component {
9079
render={() => <p>The document is less than 600px wide.</p>}
9180
/>
9281
</div>
93-
)
82+
);
9483
}
9584
}
9685
```
9786

9887
The `render` prop is never called if the query does not match.
9988

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
103-
[json2mq](https://github.com/akiran/json2mq/blob/master/README.md#usage).
89+
`<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).
10490

10591
```jsx
106-
import React from "react"
107-
import Media from "react-media"
92+
import React from "react";
93+
import Media from "react-media";
10894

10995
class App extends React.Component {
11096
render() {
@@ -120,28 +106,19 @@ class App extends React.Component {
120106
}
121107
</Media>
122108
</div>
123-
)
109+
);
124110
}
125111
}
126112
```
127113

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.
114+
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.
131115

132-
An optional `targetWindow` prop can be specified if you want the `query` to be
133-
evaluated against a different window object than the one the code is running in.
134-
This can be useful for example if you are rendering part of your component tree
135-
to an iframe or [a popup window](https://hackernoon.com/using-a-react-16-portal-to-do-something-cool-2a2d627b0202).
116+
An optional `targetWindow` prop can be specified if you want the `query` to be evaluated against a different window object than the one the code is running in. This can be useful for example if you are rendering part of your component tree to an iframe or [a popup window](https://hackernoon.com/using-a-react-16-portal-to-do-something-cool-2a2d627b0202).
136117

137-
If you're curious about how react-media differs from
138-
[react-responsive](https://github.com/contra/react-responsive), please see
139-
[this comment](https://github.com/ReactTraining/react-media/issues/70#issuecomment-347774260).
118+
If you're curious about how react-media differs from [react-responsive](https://github.com/contra/react-responsive), please see [this comment](https://github.com/ReactTraining/react-media/issues/70#issuecomment-347774260).
140119

141120
Enjoy!
142121

143122
## About
144123

145-
`react-media` is developed and maintained by [React Training](https://reacttraining.com). If you're
146-
interested in learning more about what React can do for your company, please
147-
[get in touch](mailto:[email protected])!
124+
`react-media` is developed and maintained 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])!

0 commit comments

Comments
 (0)