Last active
November 24, 2017 00:20
-
-
Save andycarrell/013c7fdb47bd00232e9797c7951821dd to your computer and use it in GitHub Desktop.
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, { Component } from 'react'; | |
| import fetch from 'unfetch' | |
| class FetchAsync extends Component { | |
| constructor(props) { | |
| super(props); | |
| this.state = { isFetching: false }; | |
| } | |
| async fetch = () => { | |
| this.setState(() => { isFetching: true }); | |
| try { | |
| const result = await fetch(this.props.uri, this.props.params); | |
| this.props.onFetched(result); | |
| } catch (err) { | |
| this.props.onError(err); | |
| } | |
| this.setState(() => { isFetching: false }); | |
| } | |
| render = () => this.props.render(this.state.isFetching); | |
| } |
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, { Component } from 'react'; | |
| import fetch from 'unfetch' | |
| class FetchAsync extends Component { | |
| constructor(props) { | |
| super(props); | |
| this.state = { isFetching: false }; | |
| } | |
| async fetch = () => { | |
| this.setState(() => { isFetching: true }); | |
| try { | |
| const result = await fetch(this.props.uri, this.props.params); | |
| this.props.onFetched(result); | |
| } catch (err) { | |
| this.props.onError(err); | |
| } | |
| this.setState(() => { isFetching: false }); | |
| } | |
| render = () => this.state.isFetching | |
| ? this.props.loadingChildren | |
| : this.props.children; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment