Skip to content

Instantly share code, notes, and snippets.

@ivawzh
Forked from hex13/render-promise-in-react.js
Created April 10, 2018 11:03
Show Gist options
  • Select an option

  • Save ivawzh/e3e9c83718ec2fcec490a7e6274cafbe to your computer and use it in GitHub Desktop.

Select an option

Save ivawzh/e3e9c83718ec2fcec490a7e6274cafbe 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>} />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment