Skip to content

Instantly share code, notes, and snippets.

@hex13
Created November 3, 2016 12:33
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save hex13/6d46f8b54631871ea8bf87576b635c49 to your computer and use it in GitHub Desktop.
Save hex13/6d46f8b54631871ea8bf87576b635c49 to your computer and use it in GitHub Desktop.
how to render promises in React
//License CC0 1.0: https://creativecommons.org/publicdomain/zero/1.0/
class Deferred extends React.Component {
constructor(props) {
super(props);
this.state = {
value: ''
};
}
componentDidMount() {
this.props.promise.then(value => {
this.setState({value});
});
}
render() {
const then = this.props.then || (value => <span>{value}</span>);
return then(this.state.value);
}
}
//...
// example
<Deferred promise={somePromise} then={v => <b>{v}</b>} />
@hex13
Copy link
Author

hex13 commented Sep 25, 2020

thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment