From d7d617374af7d196a9f893838e6f389bea1f60f6 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Thu, 17 Jan 2019 17:21:47 -0500 Subject: [PATCH] fix: Don't setState on unmounted ad (#5) Don't set state on an unmounted ad. Cribbed from https://github.com/axioscode/react-gpt/commit/9425436d0c376b35686e31fccd3cceed95da411c. --- src/Bling.js | 9 ++++++++- test/Bling.spec.js | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Bling.js b/src/Bling.js index 51f9e1e..e05d508 100644 --- a/src/Bling.js +++ b/src/Bling.js @@ -477,13 +477,20 @@ class Bling extends Component { } } + isAdMounted() { + return Bling._adManager.getMountedInstances().indexOf(this) !== -1; + } + onScriptLoaded() { const {onScriptLoaded} = this.props; if (this.getRenderWhenViewable()) { this.foldCheck(); } - this.setState({scriptLoaded: true}, onScriptLoaded); // eslint-disable-line react/no-did-mount-set-state + + if (this.isAdMounted()) { + this.setState({scriptLoaded: true}, onScriptLoaded); // eslint-disable-line react/no-did-mount-set-state + } } onScriptError(err) { diff --git a/test/Bling.spec.js b/test/Bling.spec.js index 37764d9..19eaa18 100644 --- a/test/Bling.spec.js +++ b/test/Bling.spec.js @@ -749,4 +749,19 @@ describe("Bling", () => { instance.componentWillUnmount(); expect(Bling._adManager.getMountedInstances()).to.have.length(0); }); + + it("does not call props.onSlotLoaded if it has been unmounted", () => { + const onScriptLoaded = sinon.stub(); + const instance = ReactTestUtils.renderIntoDocument( + + ); + onScriptLoaded.reset(); + instance.componentWillUnmount(); + instance.onScriptLoaded(); + expect(onScriptLoaded.called).to.be.false; + }); });