ListEmptyComponent shows a message or view when a list has no items. It helps users understand the list is empty instead of seeing a blank screen.
ListEmptyComponent in React Native
import { FlatList, Text, View } from 'react-native'; <FlatList data={dataArray} renderItem={({ item }) => <Text>{item.name}</Text>} ListEmptyComponent={() => ( <View> <Text>No items available</Text> </View> )} />
The ListEmptyComponent prop accepts a React component or a function returning a component.
This component only shows when the data array is empty.
ListEmptyComponent={() => <Text>No data found</Text>}ListEmptyComponent={<Text style={{color: 'gray'}}>Nothing here yet!</Text>}ListEmptyComponent={() => (
<View style={{alignItems: 'center'}}>
<Text>No items</Text>
<Text>Add some to get started.</Text>
</View>
)}This app shows a list with no items. Because the list is empty, the ListEmptyComponent displays a friendly message in the center.
import React from 'react'; import { FlatList, Text, View, SafeAreaView, StyleSheet } from 'react-native'; export default function App() { const emptyData = []; return ( <SafeAreaView style={styles.container}> <FlatList data={emptyData} keyExtractor={(item, index) => index.toString()} renderItem={({ item }) => <Text style={styles.item}>{item}</Text>} ListEmptyComponent={() => ( <View style={styles.emptyContainer}> <Text style={styles.emptyText}>No items to display</Text> </View> )} /> </SafeAreaView> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center' }, item: { fontSize: 18, padding: 10 }, emptyContainer: { alignItems: 'center' }, emptyText: { fontSize: 20, color: 'gray' } });
The ListEmptyComponent only shows if the data array is empty or undefined.
Time complexity is not affected by this component; it just renders conditionally.
Common mistake: forgetting to provide a keyExtractor or unique keys for list items.
Use ListEmptyComponent to improve user experience by clearly showing empty states instead of blank screens.
ListEmptyComponent shows a message or view when the list has no data.
It helps users understand the list is empty and can guide them on what to do next.
You can pass a component or a function returning a component to ListEmptyComponent.
Practice
ListEmptyComponent prop in a React Native FlatList?Solution
Step 1: Understand the role of
This prop is designed to show a special view when the list has no items to display.ListEmptyComponentStep 2: Compare with other props
Other props like styling or click handling do not relate to empty list views.Final Answer:
To display a view when the list has no data -> Option AQuick Check:
Empty list view = ListEmptyComponent [OK]
- Confusing ListEmptyComponent with item styling
- Thinking it handles clicks
- Assuming it loads more data
ListEmptyComponent in a FlatList?Solution
Step 1: Check the correct syntax for ListEmptyComponent
It accepts a component or a function returning a component. No items} /> uses a function returning aTextcomponent.Step 2: Identify incorrect usages
callsTextas a function incorrectly. Text('No items')} /> callsTextincorrectly inside the function. passes a string, which is invalid.Final Answer:
<FlatList data={[]} ListEmptyComponent={() => <Text>No items</Text>} /> -> Option CQuick Check:
Function or component = ListEmptyComponent [OK]
- Passing a string instead of a component
- Calling component as a function incorrectly
- Not using a function when needed
import React from 'react';
import { FlatList, Text } from 'react-native';
export default function MyList() {
return (
<FlatList
data={[]}
renderItem={({ item }) => <Text>{item}</Text>}
ListEmptyComponent={() => <Text>No data found</Text>}
/>
);
}Solution
Step 1: Check the data prop
The data array is empty, so no items will render.Step 2: Check ListEmptyComponent behavior
Since the list is empty, the component returned by ListEmptyComponent will display the text "No data found".Final Answer:
No data found -> Option BQuick Check:
Empty list triggers ListEmptyComponent [OK]
- Expecting no output when list is empty
- Thinking renderItem runs with empty data
- Assuming an error occurs
FlatList usage:
<FlatList
data={[]}
renderItem={({ item }) => <Text>{item}</Text>}
ListEmptyComponent={Text('No items')}
/>Solution
Step 1: Analyze ListEmptyComponent usage
It is set toText('No items'), which calls theTextfunction incorrectly instead of passing a component or function.Step 2: Check other props
renderItemis correct for empty data (though keys are recommended but not error here). Empty data is allowed.Textcan be used insideFlatList.Final Answer:
ListEmptyComponent should be a component or function, not a function call -> Option AQuick Check:
Pass component/function, not call it [OK]
- Calling component instead of passing it
- Confusing renderItem keys with ListEmptyComponent errors
- Thinking empty data is invalid
FlatList is empty. Which is the best way to implement ListEmptyComponent to achieve this?Solution
Step 1: Understand the requirement
You want a message plus a button inside the empty list view, so you need a component that returns JSX with both elements.Step 2: Evaluate options
Pass a function returning a view with text and button JSX correctly uses a function returning a view with text and button. Pass a string with the message directly toListEmptyComponentis invalid becauseListEmptyComponentdoes not accept strings. UserenderItemto render the message when data is empty misusesrenderItemwhich only renders list items. SetListEmptyComponentto null and show message outside the list moves the message outside the list, which is less integrated.Final Answer:
Pass a function returning a view with text and button JSX -> Option DQuick Check:
Use function returning JSX for custom empty view [OK]
- Passing string instead of component
- Misusing renderItem for empty state
- Placing message outside FlatList
Ready to prove it?
Pass the test and this pattern moves unknown
