Created
July 14, 2018 15:53
-
-
Save jackyef/b277ae9c47c25d24ade99fbbd0f1a8b7 to your computer and use it in GitHub Desktop.
This file contains 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 React from 'react'; | |
import { func, string } from 'prop-types'; | |
export default class Fetch extends React.Component { | |
static propTypes = { | |
render: func.isRequired, | |
url: string.isRequired, | |
}; | |
state = { | |
loading: true, | |
error: false, | |
data: null, | |
errorMessage: '', | |
}; | |
componentDidMount() { | |
const { url } = this.props; | |
fetch(url) | |
.then(res => res.json()) | |
.then(json => this.setState({ data: json, loading: false, error: false })) | |
.catch(err => this.setState({ loading: false, error: false, errorMessage: err })); | |
} | |
render() { | |
return this.props.render(this.state); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment