Last active
March 15, 2018 19:41
-
-
Save CharlesMangwa/ec7c2d0c294c5777f3f96e191dd30a5c to your computer and use it in GitHub Desktop.
React Data Fetching - Fetchcycle hooks
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 '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