Created
December 30, 2017 23:05
-
-
Save ruffle1986/c4f2b2a1c5de85df1106e90189292601 to your computer and use it in GitHub Desktop.
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
const Item = (props) => ( | |
<div className="item"> | |
<img src={ props.picture.thumbnail } /> | |
</div> | |
); | |
const List = (props) => ( | |
<div> | |
{ | |
props.items.map((item, i) => ( | |
<Item { ...item } key={ i } /> | |
)) | |
} | |
</div> | |
); | |
class ListWithFetch extends React.Component { | |
state = { | |
items: [] | |
} | |
async componentDidMount() { | |
const response = await fetch(this.props.url); | |
const { results: items} = await response.json(); | |
this.setState({ | |
items | |
}); | |
} | |
render() { | |
return this.props.children(this.state.items); | |
} | |
} | |
// and finally your app | |
ReactDOM.render( | |
<div className="app"> | |
<ListWithFetch url="https://randomuser.me/api/?results=10"> | |
{ (items) => ( | |
<List items={ items } /> | |
) } | |
</ListWithFetch> | |
</div> | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment