Created
January 19, 2018 00:44
-
-
Save JoaoCnh/05714b2a4ac4ce52a9ae96107a401a96 to your computer and use it in GitHub Desktop.
List render props
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 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