Skip to content

Instantly share code, notes, and snippets.

@allenevans
Created May 27, 2018 10:46
Show Gist options
  • Save allenevans/e1f9bd77683ea58db33862c5a438ccff to your computer and use it in GitHub Desktop.
Save allenevans/e1f9bd77683ea58db33862c5a438ccff to your computer and use it in GitHub Desktop.
Example of how to render the result of a resolved promise in react
import PropTypes from 'prop-types';
import React, { Component, Fragment } from 'react';
class AsyncFragment extends Component {
constructor(props) {
super(props);
this.state = {
value: null,
};
Promise.resolve(props.value)
.then((value) => {
this.setState({ value });
});
}
render() {
return <Fragment>{this.state.value}</Fragment>;
}
}
AsyncFragment.propTypes = {
value: PropTypes.object,
};
export default AsyncFragment;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment