Skip to content

Instantly share code, notes, and snippets.

@CharlesMangwa
Last active March 15, 2018 19:41
Show Gist options
  • Save CharlesMangwa/ec7c2d0c294c5777f3f96e191dd30a5c to your computer and use it in GitHub Desktop.
Save CharlesMangwa/ec7c2d0c294c5777f3f96e191dd30a5c to your computer and use it in GitHub Desktop.
React Data Fetching - Fetchcycle hooks
import React, { Component } from 'react'
import { Fetch } from 'react-data-fetching'
// Just here for better understanding
import { Articles, ErrorModal, Loader } from './components'
export default class News extends Component {
state = { title: 'News' }
renderContent = ({ error, data }) => {
return error
? <ErrorModal message={error} />
: <Articles posts={data.articles} />
}
reportError = (error) => {
/* You can handle your error here */
}
render() {
<View>
<Text>{this.state.title}</Text>
<Fetch
url="https://api.github.com/users/octocat"
loader={<Loader color="rainbow" />}
onLoad={() => console.log('Started fetching data...')}
onFetch={({ data }) => this.setState(() => ({ title: data.title }))}
onError={({ error }) => this.reportError(error)}
render={this.renderContent}
/>
</View>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment