Skip to content

Commit

Permalink
Add umount and check for window before mount
Browse files Browse the repository at this point in the history
  • Loading branch information
egdbear committed Dec 14, 2018
1 parent 4640704 commit 71e85d3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@ export default class MediaQuery extends React.Component {
mql = null;

componentDidMount() {
if (typeof window !== 'object') return;

const { media } = this.props;
const mediaToMatch = mediaQueries[media] || media;
this.mql = window.matchMedia(mediaToMatch);
this.mql.addListener(this.onMediaChange);

if (this.mql.matches) {
this.setState({ matched: true });
this.onMediaChange();
}
}

componentWillUnmount() {
this.mql = null;
}

onMediaChange = () => {
this.setState({ matched: this.mql.matches });
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-simple-matchmedia",
"version": "2.1.3",
"version": "2.2.0",
"description": "React component used for matching media queries",
"main": "build/index.cjs.js",
"module": "build/index.es.js",
Expand Down

0 comments on commit 71e85d3

Please sign in to comment.