Skip to content

Commit

Permalink
fix: Don't setState on unmounted ad (#5)
Browse files Browse the repository at this point in the history
Don't set state on an unmounted ad. Cribbed from
axioscode@9425436.
  • Loading branch information
andrewb committed Jan 17, 2019
1 parent 9f0fc73 commit d7d6173
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Bling.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
15 changes: 15 additions & 0 deletions test/Bling.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<Bling
adUnitPath="/4595/nfl.test.open"
slotSize={[300, 250]}
onScriptLoaded={onScriptLoaded}
/>
);
onScriptLoaded.reset();
instance.componentWillUnmount();
instance.onScriptLoaded();
expect(onScriptLoaded.called).to.be.false;
});
});

0 comments on commit d7d6173

Please sign in to comment.