-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
87 lines (78 loc) · 2.14 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// React Imports
import React, { useState, useEffect } from "react";
import {
View,
Text,
Image,
FlatList,
ActivityIndicator,
RefreshControl,
} from "react-native";
import { SafeAreaProvider } from "react-native-safe-area-context";
// API
import { getCriptoDados } from "./api/api";
// styles
import { styles } from "./styles/styles";
export default function App() {
const [dadosCripto, setDadosCripto] = useState(null);
const [carregando, setCarregando] = useState(true);
const [recarregando, setRecarregando] = useState(false);
useEffect(() => {
buscarDados();
}, []);
const buscarDados = async () => {
const data = await getCriptoDados();
setDadosCripto(data);
setCarregando(false);
};
const onRecarregar = async () => {
setRecarregando(true);
await buscarDados();
setRecarregando(false);
};
const renderizarItem = ({ item }) => {
return (
<View style={styles.criptoContainer}>
<Image
source={{
uri: `https://s2.coinmarketcap.com/static/img/coins/64x64/${item.id}.png`,
}}
style={styles.criptoLogo}
/>
<View style={styles.criptoInfo}>
<Text style={styles.criptoNome}>{item.name}</Text>
<Text style={styles.criptoPreco}>Preço: U${item.price}</Text>
<Text style={styles.criptoVariacao}>
Variação (24h): {item.change}
</Text>
</View>
</View>
);
};
if (carregando) {
return (
<View style={styles.conatinerCarregando}>
<ActivityIndicator size="large" color="#84B026" />
</View>
);
}
return (
<SafeAreaProvider>
<View style={styles.container}>
<Text style={styles.textoCabecalho}>Tabela de Criptomoedas</Text>
<FlatList
data={dadosCripto}
renderItem={renderizarItem}
keyExtractor={(item) => item.id.toString()}
contentContainerStyle={styles.conteudoFlatlist}
refreshControl={
<RefreshControl
refreshing={recarregando}
onRefresh={onRecarregar}
/>
}
/>
</View>
</SafeAreaProvider>
);
}