Skip to content

Commit

Permalink
Merge pull request #15 from capaj/guard-unmount
Browse files Browse the repository at this point in the history
guard for unmounted component when resolving/rejecting the promise
  • Loading branch information
capaj committed Jan 18, 2018
2 parents dc885b2 + 0db1772 commit 10ef362
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/react-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,20 @@ class Async extends React.Component {
})
prom.then(
res => {
this.setState({
status: statusTypes.resolved,
value: res
})
if (!this.unmounted) {
this.setState({
status: statusTypes.resolved,
value: res
})
}
},
err => {
this.setState({
status: statusTypes.rejected,
value: err
})
if (!this.unmounted) {
this.setState({
status: statusTypes.rejected,
value: err
})
}
}
)
}
Expand All @@ -47,6 +51,10 @@ class Async extends React.Component {
this.handlePromise(this.props.promise)
}
}
componentWillUnmount () {
this.unmounted = true
}

render () {
const { props, state } = this

Expand Down

0 comments on commit 10ef362

Please sign in to comment.