Skip to content

Instantly share code, notes, and snippets.

@ruffle1986
Created December 30, 2017 23:05
Show Gist options
  • Save ruffle1986/c4f2b2a1c5de85df1106e90189292601 to your computer and use it in GitHub Desktop.
Save ruffle1986/c4f2b2a1c5de85df1106e90189292601 to your computer and use it in GitHub Desktop.
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