Skip to content

Instantly share code, notes, and snippets.

@dwijonarko
Created February 18, 2019 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dwijonarko/398e70d80bc537ae63afe9c80afc25c3 to your computer and use it in GitHub Desktop.
Save dwijonarko/398e70d80bc537ae63afe9c80afc25c3 to your computer and use it in GitHub Desktop.
React Native Networking
import React, { Component } from 'react';
import { View, Text,Image,ActivityIndicator,FlatList } from 'react-native';
export default class PeoplePage extends Component {
constructor(props) {
super(props);
this.state = {
dataPeople:[],
isLoading:true
};
}
componentDidMount = () => {
this._fetchItem()
}
_fetchItem = ()=>{
return fetch('https://randomuser.me/api/?results=20')
.then((response)=>response.json())
.then((responseJson)=>{
this.setState({
isLoading:false,
dataPeople:responseJson.results
});
})
.catch((error)=>{
console.error(error)
})
}
_itemCoponent=({item})=>{
return(
<View style={{flex:1,flexDirection: 'row',marginLeft: 10,}} >
<View style={{height:50,justifyContent: 'center',}}>
<Image source={{uri: item.picture.thumbnail }}
style={{width:40,height:40,borderRadius: 25,}}
/>
</View>
<View style={{flex:2,height:50}}>
<Text style={{padding:5}} >{item.name.first} {item.name.last}</Text>
<Text style={{ padding: 5 }}>{item.name.email}</Text>
</View>
</View>
)
}
_separatorComponent=()=>{
return(
<View style={{backgroundColor:'grey',height:0.5}}></View>
)
}
render() {
if (this.state.isLoading) {
return(
<View style={{padding:20}}>
<ActivityIndicator/>
</View>
)
}
return (
<View style={{paddingTop: 20,}}>
<FlatList
data={this.state.dataPeople}
renderItem={this._itemCoponent}
keyExtractor={(item,index)=>index.toString()}
ItemSeparatorComponent={this._separatorComponent}
onRefresh={this._fetchItem}
refreshing={this.state.isLoading}
/>
</View>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment