Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
React PromiseMixin
var PENDING = 0,
FULFILLED = 1,
REJECTED = 2;
export var PromiseMixin = {
getInitialState: function() {
return {promiseState: PENDING};
},
componentWillMount: function() {
this.getPromise().then(function(value) {
this.setState({
promiseState: FULFILLED,
promiseValue: value
}, function() {
if (this.onFulfilled) {
this.onFulfilled(value);
}
}.bind(this));
}.bind(this), function(reason) {
this.setState({
promiseState: REJECTED,
promiseRejectReason: reason
}, function() {
if (this.onRejected) {
this.onRejected(reason);
}
}.bind(this));
}.bind(this));
},
isFulfilled: function() {
return this.state.promiseState == FULFILLED;
},
render: function() {
switch (this.state.promiseState) {
case PENDING:
return this.renderPending();
case FULFILLED:
return this.renderFulfilled(this.state.promiseValue);
case REJECTED:
return this.renderRejected(this.state.promiseRejectReason);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.