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

Method constructor

modules/Media.js:33–88  ·  view source on GitHub ↗
(props)

Source from the content-addressed store, hash-verified

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 }
75
76 this.initialize();
77
78 // Instead of calling this.updateMatches, we manually set the initial state to prevent
79 // calling setState, which could trigger an unnecessary second render
80 this.state = {
81 matches:
82 this.props.defaultMatches !== undefined
83 ? this.props.defaultMatches
84 : this.getMatches()
85 };
86
87 this.onChange();
88 }
89
90 getMatches = () => {

Callers

nothing calls this directly

Calls 2

initializeMethod · 0.95
onChangeMethod · 0.95

Tested by

no test coverage detected