Promised should keep track of mounting status

This commit is contained in:
Vesa Luusua 2017-07-10 14:09:30 +03:00
parent 6e246a2f58
commit a080c52663

View file

@ -6,6 +6,7 @@
* <Promised promise={givenPromise} renderFulfilled={v => <b>{v}</b>} renderRejected={v => <b>v</b>} />
*/
/* eslint-disable no-underscore-dangle */
import { Component, PropTypes } from 'react';
class Promised extends Component {
@ -17,18 +18,28 @@ class Promised extends Component {
value: '',
error: null,
};
this._isMounted = false;
}
componentDidMount() {
this._isMounted = true;
this.props.promise
.then(value => {
this.setState({ value });
if (this._isMounted) {
this.setState({ value });
}
})
.catch(error => {
this.setState({ error });
if (this._isMounted) {
this.setState({ error });
}
});
}
componentWillUnmount() {
this._isMounted = false;
}
render() {
const { renderFulfilled, renderRejected } = this.props;
return this.state.error ? renderRejected(this.state.error) : renderFulfilled(this.state.value);