Skip to content

Instantly share code, notes, and snippets.

@JoaoCnh
Created January 19, 2018 00:44
Show Gist options
  • Save JoaoCnh/05714b2a4ac4ce52a9ae96107a401a96 to your computer and use it in GitHub Desktop.
Save JoaoCnh/05714b2a4ac4ce52a9ae96107a401a96 to your computer and use it in GitHub Desktop.
List render props
import React from "react";
import PropTypes from "prop-types";
class List extends React.Component {
static propTypes = {
render: PropTypes.func.isRequired,
url: PropTypes.string.isRequired,
};
state = {
list: [],
isLoading: false,
};
_fetch = async () => {
const res = await fetch(this.props.url);
const json = await res.json();
this.setState({
list: json,
isLoading: false,
});
}
componentDidMount() {
this.setState({ isLoading: true }, this._fetch);
}
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