From 71e85d33b555b3625afa6f7758c130573a07840d Mon Sep 17 00:00:00 2001 From: egdbear Date: Fri, 14 Dec 2018 12:23:55 +0100 Subject: [PATCH] Add umount and check for window before mount --- index.js | 9 ++++++++- package.json | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index bb9bfce..c41019f 100644 --- a/index.js +++ b/index.js @@ -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 }); } diff --git a/package.json b/package.json index 9d61642..6ffb32c 100644 --- a/package.json +++ b/package.json @@ -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",