Skip to content

Commit fe6ab45

Browse files
author
MICHAEL JACKSON
committed
Use invariant instead of throw Error
1 parent 2d0a766 commit fe6ab45

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

modules/Media.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from "react";
22
import PropTypes from "prop-types";
3+
import invariant from "invariant";
34
import json2mq from "json2mq";
45

56
/**
@@ -31,15 +32,14 @@ class Media extends React.Component {
3132
componentWillMount() {
3233
if (typeof window !== "object") return;
3334

34-
let { query } = this.props;
3535
const targetWindow = this.props.targetWindow || window;
3636

37-
if (!targetWindow.matchMedia) {
38-
throw new Error(
39-
'You passed a `targetWindow` prop to `Media` that does not have a `matchMedia` function.'
40-
);
41-
}
37+
invariant(
38+
typeof targetWindow.matchMedia === "function",
39+
"<Media targetWindow> does not support `matchMedia`."
40+
);
4241

42+
let { query } = this.props;
4343
if (typeof query !== "string") query = json2mq(query);
4444

4545
this.mediaQueryList = targetWindow.matchMedia(query);

modules/__tests__/Media-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ describe("A <Media>", () => {
155155

156156
expect(() => {
157157
ReactDOM.render(element, node, () => {});
158-
}).toThrow("does not have a `matchMedia` function");
158+
}).toThrow("does not support `matchMedia`");
159159
});
160-
})
160+
});
161161
});
162162

163163
describe("rendered on the server", () => {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"react": ">=15 || ^0.14.7"
2525
},
2626
"dependencies": {
27+
"invariant": "^2.2.2",
2728
"json2mq": "^0.2.0",
2829
"prop-types": "^15.5.10"
2930
},

0 commit comments

Comments
 (0)