Skip to content

Instantly share code, notes, and snippets.

@jackyef
Created July 14, 2018 15:53
Show Gist options
  • Save jackyef/b277ae9c47c25d24ade99fbbd0f1a8b7 to your computer and use it in GitHub Desktop.
Save jackyef/b277ae9c47c25d24ade99fbbd0f1a8b7 to your computer and use it in GitHub Desktop.
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