MCPcopy Create free account
hub / github.com/ReactTraining/react-media / Media

Class Media

modules/Media.js:17–183  ·  view source on GitHub ↗

* Conditionally renders based on whether or not a media query matches.

Source from the content-addressed store, hash-verified

15 * Conditionally renders based on whether or not a media query matches.
16 */
17class Media extends React.Component {
18 static propTypes = {
19 defaultMatches: PropTypes.oneOfType([
20 PropTypes.bool,
21 PropTypes.objectOf(PropTypes.bool)
22 ]),
23 query: queryType,
24 queries: PropTypes.objectOf(queryType),
25 render: PropTypes.func,
26 children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
27 targetWindow: PropTypes.object,
28 onChange: PropTypes.func
29 };
30
31 queries = [];
32
33 constructor(props) {
34 super(props);
35
36 invariant(
37 !(!props.query && !props.queries) || (props.query && props.queries),
38 '<Media> must be supplied with either "query" or "queries"'
39 );
40
41 invariant(
42 props.defaultMatches === undefined ||
43 !props.query ||
44 typeof props.defaultMatches === "boolean",
45 "<Media> when query is set, defaultMatches must be a boolean, received " +
46 typeof props.defaultMatches
47 );
48
49 invariant(
50 props.defaultMatches === undefined ||
51 !props.queries ||
52 typeof props.defaultMatches === "object",
53 "<Media> when queries is set, defaultMatches must be a object of booleans, received " +
54 typeof props.defaultMatches
55 );
56
57 if (typeof window !== "object") {
58 // In case we're rendering on the server, apply the default matches
59 let matches;
60 if (props.defaultMatches !== undefined) {
61 matches = props.defaultMatches;
62 } else if (props.query) {
63 matches = true;
64 } /* if (props.queries) */ else {
65 matches = Object.keys(this.props.queries).reduce(
66 (acc, key) => ({ ...acc, [key]: true }),
67 {}
68 );
69 }
70 this.state = {
71 matches
72 };
73 return;
74 }

Callers

nothing calls this directly

Calls 1

unwrapSingleQueryFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…