Last active
February 24, 2018 04:53
-
-
Save jaycosaur/cd4fff6ed62da48040b302f4db5923ab to your computer and use it in GitHub Desktop.
React Fetch Render Props Example
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
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