Advertisement
notaduck

SomeScreen.js

Nov 27th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from "react";
  2. import { StyleSheet, View, Text, ActivityIndicator } from "react-native";
  3. import axios from 'axios'
  4.  
  5. export default class SomeScreen extends React.Component {
  6.   state = {
  7.     loading: false,
  8.     products: []
  9.   };
  10.  
  11.   async componentDidMount() {
  12.     await this.fetchProducts()
  13.   }
  14.  
  15.   async fetchProducts() {
  16.     const url = "your url"
  17.     axios.get(url).then(response => response.data)
  18.       .then((data) => {
  19.         this.setState({ markers: data, loading: true })
  20.         // console.log(data)
  21.       })
  22.  
  23.   }
  24.  
  25.   render() {
  26.     const { loading, products } = this.state;
  27.  
  28.     if (loading) {
  29.       return (
  30.  
  31.         <View style={styles.loading}>
  32.           <ActivityIndicator size="large" color="#fff" />
  33.           <Text style={styles.loadingText}> Please wait....</Text>
  34.         </View>
  35.  
  36.       );
  37.     } else
  38.       return (
  39.  
  40.         <View style={styles.container}>
  41.           {products.map(product => {
  42.             console.info(product)
  43.             return (
  44.               <Button
  45.                 title={product.xxxxx}
  46.                 onPress={() => console.info("click")}
  47.               />
  48.             )
  49.           })}
  50.         </View >
  51.  
  52.       );
  53.   }
  54. }
  55.  
  56. const styles = StyleSheet.create({
  57.   container: {
  58.     flex: 1,
  59.     backgroundColor: "#fff"
  60.   },
  61.   loading: {
  62.     flex: 1,
  63.     backgroundColor: "#34495E",
  64.     alignItems: "center",
  65.     justifyContent: "center"
  66.   },
  67.   loadingText: {
  68.     fontSize: 32,
  69.     color: "#fff"
  70.   }
  71. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement