import React from 'react';
import { View, Text, FlatList, StyleSheet } from 'react-native';
export default function SimpleList() {
const data = [];
const renderEmpty = () => (
<View style={styles.emptyContainer}>
<Text style={styles.emptyText}>No items available.</Text>
</View>
);
return (
<View style={styles.container}>
<FlatList
data={data}
keyExtractor={(item, index) => index.toString()}
renderItem={({ item }) => <Text style={styles.item}>{item}</Text>}
ListEmptyComponent={renderEmpty}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: 50,
paddingHorizontal: 20
},
item: {
fontSize: 18,
paddingVertical: 10
},
emptyContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
marginTop: 50
},
emptyText: {
fontSize: 18,
color: 'gray',
textAlign: 'center'
}
});We added the ListEmptyComponent prop to the FlatList. This prop takes a component that will be shown when the list has no items.
The renderEmpty function returns a View with a Text inside that says "No items available." We styled it to be gray and centered so it looks friendly and clear.
This way, when the data array is empty, the user sees a helpful message instead of a blank screen.