Skip to content

Instantly share code, notes, and snippets.

@andycarrell
Last active November 24, 2017 00:20
Show Gist options
  • Select an option

  • Save andycarrell/013c7fdb47bd00232e9797c7951821dd to your computer and use it in GitHub Desktop.

Select an option

Save andycarrell/013c7fdb47bd00232e9797c7951821dd to your computer and use it in GitHub Desktop.
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);
}
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