Skip to content

Commit

Permalink
Add remove listener
Browse files Browse the repository at this point in the history
  • Loading branch information
egdbear committed May 2, 2019
1 parent 71e85d3 commit be92523
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ import PropTypes from 'prop-types';
import mediaQueries from './mediaQueries';

export default class MediaQuery extends React.Component {
mql = null;

active = true;

state = {
matched: false,
}

mql = null;

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

const { media } = this.props;
const mediaToMatch = mediaQueries[media] || media;
Expand All @@ -23,10 +26,14 @@ export default class MediaQuery extends React.Component {
}

componentWillUnmount() {
this.active = false;
this.mql.removeListener(this.onMediaChange);
this.mql = null;
}

onMediaChange = () => {
if (!this.active) return;

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

Expand Down

0 comments on commit be92523

Please sign in to comment.