Skip to content

Instantly share code, notes, and snippets.

@jaycosaur
Last active February 24, 2018 04:53
Show Gist options
  • Save jaycosaur/cd4fff6ed62da48040b302f4db5923ab to your computer and use it in GitHub Desktop.
Save jaycosaur/cd4fff6ed62da48040b302f4db5923ab to your computer and use it in GitHub Desktop.
React Fetch Render Props Example
class APIFetch extends Component {
constructor(props){
super(props)
this.state = {
data: null,
isError: false,
isFetching: true
}
}
async componentWillMount(){
const header = this.props.optionalHeader?this.props.optionalHeader:null
await fetch(this.props.fetchPath, header)
.then(response => response.json())
.then(data => this.setState({data: data}))
.catch((err) => this.setState({isError: 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