Skip to content

Instantly share code, notes, and snippets.

@mdshadman
Created October 24, 2019 18:04
Show Gist options
  • Save mdshadman/8ae1cf0f60c153653c7be0cf24fed573 to your computer and use it in GitHub Desktop.
Save mdshadman/8ae1cf0f60c153653c7be0cf24fed573 to your computer and use it in GitHub Desktop.
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