Skip to content

Instantly share code, notes, and snippets.

@itsMapleLeaf
Last active January 26, 2019 20:13
Show Gist options
  • Save itsMapleLeaf/ffdcb6d9af7a7710ab8636f200014ba2 to your computer and use it in GitHub Desktop.
Save itsMapleLeaf/ffdcb6d9af7a7710ab8636f200014ba2 to your computer and use it in GitHub Desktop.
import * as React from 'react'
class Fetcher extends React.Component {
constructor() {
this.state = {
loading: true,
data: {},
}
}
componentDidMount() {
fetch(this.props.url)
.then(res => res.json())
.then(data => {
this.setState({ loading: false, data })
})
}
render() {
const { loading, data } = this.state
if (loading) {
return <div>loading...</div>
}
return this.props.children({ data })
}
}
const UserProfile = () => (
<Fetcher url="/api/user">
{props => (
<div>
<h1>{props.data.username}</h1>
<p>{props.data.profile}</p>
</div>
)}
</Fetcher>
)
const TweetList = () => (
<Fetcher url="/api/tweets">
{props =>
props.data.tweets.map(tweet => (
<span key={tweet.id}>
{tweet.user}: {tweet.body}
</span>
))
}
</Fetcher>
)
@xadiadi
Copy link

xadiadi commented Jan 26, 2019

thanks

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