Skip to content

Commit 1defdb3

Browse files
committed
Added check so libray doesn't call setState on component that is not mounted.
1 parent ab5ef30 commit 1defdb3

1 file changed

Lines changed: 17 additions & 8 deletions

File tree

src/index.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,30 @@ const scriptLoader = (...scripts) => (WrappedComponent) => {
8282
isScriptLoaded: false,
8383
isScriptLoadSucceed: false
8484
}
85+
86+
this._isMounted = false;
8587
}
8688

8789
componentDidMount () {
90+
this._isMounted = true;
8891
startLoadingScripts(scripts, err => {
89-
this.setState({
90-
isScriptLoaded: true,
91-
isScriptLoadSucceed: !err
92-
}, () => {
93-
if (!err) {
94-
this.props.onScriptLoaded()
95-
}
96-
})
92+
if(this._isMounted) {
93+
this.setState({
94+
isScriptLoaded: true,
95+
isScriptLoadSucceed: !err
96+
}, () => {
97+
if (!err) {
98+
this.props.onScriptLoaded()
99+
}
100+
})
101+
}
97102
})
98103
}
99104

105+
componentWillUnmount () {
106+
this._isMounted = false;
107+
}
108+
100109
render () {
101110
const props = {
102111
...this.props,

0 commit comments

Comments
 (0)