Created
October 24, 2019 18:04
-
-
Save mdshadman/8ae1cf0f60c153653c7be0cf24fed573 to your computer and use it in GitHub Desktop.
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 ApiView from './ApiView'; | |
import axios from 'axios'; | |
import styles from './ApiStyles'; | |
import { | |
StyleSheet, | |
View, | |
ActivityIndicator, | |
FlatList, | |
Text, | |
TouchableOpacity | |
} from "react-native"; | |
class ApiContainer extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
loading: false, | |
fromFetch: false, | |
fromAxios: false, | |
dataSource: [], | |
axiosData: null | |
}; | |
} | |
goForFetch = () => { | |
this.setState({ | |
fromFetch: true, | |
loading: true, | |
}) | |
fetch("https://jsonplaceholder.typicode.com/users") | |
.then(response => response.json()) | |
.then((responseJson) => { | |
console.log('getting data from fetch', responseJson) | |
this.setState({ | |
loading: false, | |
dataSource: responseJson | |
}) | |
}) | |
.catch(error => console.log(error)) | |
} | |
goForAxios = () => { | |
this.setState({ | |
fromFetch: false, | |
loading: true, | |
}) | |
axios.get("https://jsonplaceholder.typicode.com/users") | |
.then(response => { | |
console.log('getting data from axios', response.data); | |
this.setState({ | |
loading: false, | |
axiosData: response.data | |
}) | |
}) | |
.catch(error => { | |
console.log(error); | |
}); | |
} | |
FlatListSeparator = () => { | |
return ( | |
<View style={{ | |
height: .5, | |
width: "100%", | |
backgroundColor: "rgba(0,0,0,0.5)", | |
}} | |
/> | |
); | |
} | |
renderItem = (data) => { | |
return ( | |
<TouchableOpacity style={styles.list}> | |
<Text style={styles.lightText}>{data.item.name}</Text> | |
<Text style={styles.lightText}>{data.item.email}</Text> | |
<Text style={styles.lightText}>{data.item.company.name}</Text></TouchableOpacity> | |
) | |
} | |
render() { | |
const { dataSource, fromFetch, fromAxios, loading, axiosData } = this.state | |
return ( | |
<ApiView | |
goForFetch={this.goForFetch} | |
goForAxios={this.goForAxios} | |
dataSource={dataSource} | |
loading={loading} | |
fromFetch={fromFetch} | |
fromAxios={fromAxios} | |
axiosData={axiosData} | |
FlatListSeparator={this.FlatListSeparator} | |
renderItem={this.renderItem} | |
/> | |
); | |
} | |
} | |
export default ApiContainer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment