Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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>} />
@dylanfpaul

This comment has been minimized.

Copy link

@dylanfpaul dylanfpaul commented Sep 7, 2018

Interesting prospect. This could be useful for container components that then populate child markup with fetched data.

@AbhayShiro

This comment has been minimized.

Copy link

@AbhayShiro AbhayShiro commented Sep 25, 2020

Bro u are the best

@hex13

This comment has been minimized.

Copy link
Owner Author

@hex13 hex13 commented Sep 25, 2020

thanks :)

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