Item separators help to clearly show the space or line between list items. They make lists easier to read and look neat.
Item separators in React Native
const renderSeparator = () => {
return <View style={{ height: 1, backgroundColor: '#CED0CE' }} />;
};
<FlatList
data={data}
renderItem={renderItem}
ItemSeparatorComponent={renderSeparator}
/>The ItemSeparatorComponent prop in FlatList is used to add a separator between items.
The separator is usually a thin View with a background color and height.
const Separator = () => <View style={{ height: 1, backgroundColor: 'gray' }} />;
<FlatList
data={items}
renderItem={({ item }) => <Text>{item}</Text>}
ItemSeparatorComponent={Separator}
/>const Separator = () => <View style={{ height: 2, backgroundColor: 'blue', marginVertical: 5 }} />;
<FlatList
data={items}
renderItem={({ item }) => <Text>{item}</Text>}
ItemSeparatorComponent={Separator}
/>This app shows a list of fruits with a thin gray line separating each fruit name. The separator helps to see each item clearly.
import React from 'react'; import { FlatList, Text, View, SafeAreaView } from 'react-native'; const data = ['Apple', 'Banana', 'Cherry', 'Date']; const ItemSeparator = () => { return <View style={{ height: 1, backgroundColor: '#CCCCCC' }} />; }; export default function App() { const renderItem = ({ item }) => <Text style={{ padding: 20, fontSize: 18 }}>{item}</Text>; return ( <SafeAreaView style={{ flex: 1, marginTop: 50 }}> <FlatList data={data} renderItem={renderItem} keyExtractor={(item) => item} ItemSeparatorComponent={ItemSeparator} /> </SafeAreaView> ); }
You can customize the separator style to match your app's design.
If you don't add a separator, the list items will appear without any dividing lines.
Separators only appear between items, not before the first or after the last item.
Item separators visually divide list items for clarity.
Use ItemSeparatorComponent in FlatList to add separators.
Separators are usually thin View components with background color and height.
Practice
ItemSeparatorComponent in a FlatList in React Native?Solution
Step 1: Understand the role of
This prop is used to add a visual separator between items in a list.ItemSeparatorComponentStep 2: Identify the effect on UI
It improves readability by visually dividing items, not for sorting or animations.Final Answer:
To visually separate list items for better readability -> Option AQuick Check:
ItemSeparatorComponent = Visual separation [OK]
- Confusing separators with sorting or animations
- Thinking separators load more data
- Assuming separators change item order
FlatList?Solution
Step 1: Identify the correct prop name
The correct prop isItemSeparatorComponentinFlatList.Step 2: Check the separator style
A horizontal separator uses a small height and a background color, typically height: 1.Final Answer:
ItemSeparatorComponent={() => <View style={{height: 1, backgroundColor: 'gray'}} />}-> Option CQuick Check:
Correct prop and style =ItemSeparatorComponent={() => <View style={{height: 1, backgroundColor: 'gray'}} />}[OK]
- Using wrong prop names like separator or SeparatorComponent
- Setting width instead of height for horizontal separators
- Using too large height or wrong colors
FlatList?
const data = ['Apple', 'Banana', 'Cherry'];
<FlatList
data={data}
renderItem={({item}) => <Text>{item}</Text>}
ItemSeparatorComponent={() => <View style={{height: 2, backgroundColor: 'black'}} />}
/>Solution
Step 1: Analyze the separator component
The separator is aViewwith height 2 and black background, so a thin black line.Step 2: Understand separator placement
Separators appear between each item, not after the last one.Final Answer:
A list of fruits with a thin black line between each item -> Option DQuick Check:
ItemSeparatorComponent creates black lines between items [OK]
- Thinking separators appear after last item
- Confusing separator color or thickness
- Assuming no separators show without extra code
FlatList code that tries to add separators?
<FlatList
data={[1,2,3]}
renderItem={({item}) => <Text>Item {item}</Text>}
ItemSeparatorComponent="<View style={{height: 1, backgroundColor: 'gray'}} />"
/>Solution
Step 1: Check how ItemSeparatorComponent is passed
It is passed as a string, which is incorrect. It must be a function returning a component.Step 2: Verify other parts
Data and renderItem are correct; height is correct for horizontal separator.Final Answer:
The separator is passed as a string instead of a function component -> Option AQuick Check:
ItemSeparatorComponent must be a function, not a string [OK]
- Passing JSX as string instead of function
- Confusing height and width for separator style
- Incorrect renderItem syntax
FlatList with vertical items and a custom separator that is a 10-pixel tall red line with 20 pixels horizontal margin on each side. Which code snippet correctly implements this separator?Solution
Step 1: Understand vertical list separator style
For vertical lists, separator height controls thickness; width is full width by default.Step 2: Apply margin for horizontal spacing
UsemarginHorizontal: 20to add space on left and right sides.Step 3: Check color and size
Height 10 and backgroundColor red matches the requirement.Final Answer:
ItemSeparatorComponent={() => <View style={{height: 10, backgroundColor: 'red', marginHorizontal: 20}} />} -> Option BQuick Check:
Vertical separator uses height + marginHorizontal [OK]
- Using width instead of height for vertical separators
- Using marginVertical instead of marginHorizontal for side margins
- Confusing margin directions
Ready to prove it?
Pass the test and this pattern moves unknown
