Created
November 3, 2016 12:33
-
-
Save hex13/6d46f8b54631871ea8bf87576b635c49 to your computer and use it in GitHub Desktop.
how to render promises in React
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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>} /> | |
Bro u are the best
thanks :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Interesting prospect. This could be useful for container components that then populate child markup with fetched data.