From 081b9162e250fa96858e2fad39f1b7ce8e36caec Mon Sep 17 00:00:00 2001 From: meriadec Date: Mon, 7 Mar 2016 10:05:42 +0100 Subject: [PATCH] prevent calling setState after component unmount --- lib/portal.js | 8 +++++--- test/portal_spec.js | 9 +++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/portal.js b/lib/portal.js index b2a00c8..d9370e5 100644 --- a/lib/portal.js +++ b/lib/portal.js @@ -66,7 +66,7 @@ export default class Portal extends React.Component { document.removeEventListener('touchstart', this.handleOutsideMouseClick); } - this.closePortal(); + this.closePortal(true); } shouldComponentUpdate(nextProps, nextState) { @@ -108,7 +108,7 @@ export default class Portal extends React.Component { this.props.onOpen(this.node); } - closePortal() { + closePortal(isUnmounted = false) { const resetPortalState = () => { if (this.node) { ReactDOM.unmountComponentAtNode(this.node); @@ -116,7 +116,9 @@ export default class Portal extends React.Component { } this.portal = null; this.node = null; - this.setState({active: false}); + if (!isUnmounted) { + this.setState({active: false}); + } }; if (this.state.active) { diff --git a/test/portal_spec.js b/test/portal_spec.js index 72b29c3..132fa1e 100644 --- a/test/portal_spec.js +++ b/test/portal_spec.js @@ -155,6 +155,15 @@ describe('react-portal', () => { unmountComponentAtNode(div); assert(props.onClose.calledOnce); }); + + it('should not call this.setState() if portal is unmounted', () => { + const div = document.createElement('div'); + const props = {isOpened: true}; + const wrapper = render(

Hi

, div); + spy(wrapper, 'setState'); + unmountComponentAtNode(div); + assert.equal(wrapper.setState.callCount, 0); + }); }); describe('openByClickOn', () => {