mirror of
https://github.com/kingomarnajjar/flex-template-web.git
synced 2026-07-27 19:42:11 +10:00
Promised should keep track of mounting status
This commit is contained in:
parent
6e246a2f58
commit
a080c52663
1 changed files with 13 additions and 2 deletions
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue