Skip to content

Instantly share code, notes, and snippets.

@amandeepmittal
Created February 9, 2019 12:33
Show Gist options
  • Save amandeepmittal/bcfa80d51cf5c7de0208a685064222ce to your computer and use it in GitHub Desktop.
Save amandeepmittal/bcfa80d51cf5c7de0208a685064222ce to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import { FlatList } from 'react-native';
import { getUSANews } from '../utils/fetchNews';
import Article from './Article';
class News extends Component {
state = {
articles: [],
refreshing: true
};
componentDidMount = () => {
this.fetchNews();
};
fetchNews = () => {
getUSANews()
.then(articles => {
this.setState({ articles, refreshing: false });
})
.catch(() => this.setState({ refreshing: false }));
};
handleRefresh = () => {
this.setState({ refreshing: true }, () => this.fetchNews());
};
render() {
return (
<FlatList
data={this.state.articles}
renderItem={({ item }) => <Article article={item} />}
keyExtractor={item => item.url}
refreshing={this.state.refreshing}
onRefresh={this.handleRefresh}
/>
);
}
}
export default News;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment