Last active
January 26, 2019 20:13
-
-
Save itsMapleLeaf/ffdcb6d9af7a7710ab8636f200014ba2 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
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> | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks