Skip to content

Commit

Permalink
Merge pull request #109 from modosc/fix-error
Browse files Browse the repository at this point in the history
fix Cannot read property 'removeListener' of undefined
  • Loading branch information
contra committed Jul 14, 2017
2 parents 83c2409 + ea3d577 commit 694f18e
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 16 deletions.
17 changes: 10 additions & 7 deletions dist/react-responsive.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/react-responsive.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/react-responsive.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
],
"dependencies": {
"hyphenate-style-name": "^1.0.0",
"matchmediaquery": "^0.2.0",
"matchmediaquery": "^0.2.1",
"prop-types": "^15.5.7"
},
"peerDependencies": {
Expand Down
15 changes: 9 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ export default class MediaQuery extends React.Component {
}, {})
}

if (this._mql) {
this._mql.removeListener(this.updateMatches)
this._mql.dispose()
}
this.removeMql()

this._mql = matchMedia(this.query, values)
this._mql.addListener(this.updateMatches)
Expand All @@ -83,8 +80,7 @@ export default class MediaQuery extends React.Component {
}

componentWillUnmount() {
this._mql.removeListener(this.updateMatches)
this._mql.dispose()
this.removeMql()
}

updateMatches = () => {
Expand All @@ -96,6 +92,13 @@ export default class MediaQuery extends React.Component {
})
}

removeMql = () => {
if (this._mql) {
this._mql.removeListener(this.updateMatches)
this._mql.dispose()
}
}

render() {
if(typeof this.props.children === 'function') {
return this.props.children(this.state.matches)
Expand Down
11 changes: 11 additions & 0 deletions test/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const MediaQuery = require('index')
const mm = { default: require('matchmediaquery') }
const assert = require('chai').assert
const sinon = require('sinon')
const ReactDOM = require('react-dom')
const TestUtils = require('react-dom/test-utils')

describe('MediaQuery', function () {
Expand Down Expand Up @@ -200,4 +201,14 @@ describe('MediaQuery', function () {
e.setState({ matches: false })
assert.isNotFalse(onBeforeChangeCallback.calledBefore(onChangeCallback))
})
it('handles unmount', function () {
const container = document.createElement('div')
const mq = (
<MediaQuery query="all">
<div className="childComponent"/>
</MediaQuery>
)
ReactDOM.render(mq, container)
assert.doesNotThrow(() => ReactDOM.unmountComponentAtNode(container))
})
})

0 comments on commit 694f18e

Please sign in to comment.