I'm trying to re-fetch new data on Button Click in React Native. How to do that?
This is my current code and It's not fetching new content on Button Click, Instead nothing happening.
I have done until it's fetching content from API but can't implement refresh on button click because Flatlist is not loading again and again.
Thanks in Advance.
import React, { useEffect, useState } from 'react';
import { ActivityIndicator, FlatList, StyleSheet, View, Text, Button } from 'react-native';
import {
Colors
} from 'react-native/Libraries/NewAppScreen';
const App: () => React$Node = () => {
const [isLoading, setLoading] = useState(true);
const [data, setData] = useState([]);
useEffect(() => {
fetch('https://exampleapi.dev/')
.then((response) => response.json())
.then((json) => setData(json))
.catch((error) => console.error(error))
.finally(() => setLoading(false));
}, []);
return (
<>
<View style={styles.container}>
{isLoading ? <ActivityIndicator/> : (
<FlatList
data={data}
keyExtractor={({ id }, index) => id}
renderItem={({ item }) => (
<Text style={styles.content}>{item.content}</Text>
)}
/>
)}
</View>
<View style={styles.buttonBottom}>
<Button
title="🔀 Refresh"
onPress={() => this.FlatList}
style={styles.buttonShare}
color="#66BB6A" />
</View>
</>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'space-between',
backgroundColor: '#1b262c',
padding: 30,
flexDirection:'row',
alignItems:'center'
},
FlatList: {
backgroundColor: Colors.aquamarine,
},
content: {
fontSize: 22,
textAlign: 'left',
color: '#bbe1fa'
},
buttonBottom: {
fontSize: 22,
padding: 10,
backgroundColor: '#1b262c',
}
});
export default App;