Build: Item Separator List
This screen shows a list of items with a visible separator line between each item.
Target UI
--------------------- | Item 1 | --------------------- | Item 2 | --------------------- | Item 3 | ---------------------
--------------------- | Item 1 | --------------------- | Item 2 | --------------------- | Item 3 | ---------------------
import React from 'react'; import { FlatList, Text, View, StyleSheet } from 'react-native'; export default function ItemSeparatorList() { const data = [ { id: '1', title: 'Item 1' }, { id: '2', title: 'Item 2' }, { id: '3', title: 'Item 3' }, { id: '4', title: 'Item 4' }, { id: '5', title: 'Item 5' } ]; return ( <View style={styles.container}> <FlatList data={data} keyExtractor={item => item.id} renderItem={({ item }) => <Text style={styles.item}>{item.title}</Text>} // TODO: Add item separator component here /> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, marginTop: 50 }, item: { padding: 20, fontSize: 18 } });
import React from 'react'; import { FlatList, Text, View, StyleSheet } from 'react-native'; export default function ItemSeparatorList() { const data = [ { id: '1', title: 'Item 1' }, { id: '2', title: 'Item 2' }, { id: '3', title: 'Item 3' }, { id: '4', title: 'Item 4' }, { id: '5', title: 'Item 5' } ]; const renderSeparator = () => { return <View style={styles.separator} />; }; return ( <View style={styles.container}> <FlatList data={data} keyExtractor={item => item.id} renderItem={({ item }) => <Text style={styles.item}>{item.title}</Text>} ItemSeparatorComponent={renderSeparator} /> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, marginTop: 50 }, item: { padding: 20, fontSize: 18 }, separator: { height: 1, backgroundColor: '#CCCCCC', marginLeft: 0, marginRight: 0 } });
We added the ItemSeparatorComponent prop to the FlatList. This prop takes a component that renders between each item. We created a simple renderSeparator function that returns a View styled as a thin gray horizontal line. This line appears between each item in the list, making the list easier to read and visually separated.
The separator uses a height of 1 and a light gray color. It spans the full width because margins are set to zero. This is a common pattern to improve list readability in mobile apps.
--------------------- | Item 1 | --------------------- | Item 2 | --------------------- | Item 3 | --------------------- | Item 4 | --------------------- | Item 5 | ---------------------
ItemSeparatorComponent in a FlatList in React Native?ItemSeparatorComponentFlatList?ItemSeparatorComponent in FlatList.ItemSeparatorComponent={() => <View style={{height: 1, backgroundColor: 'gray'}} />} -> Option CItemSeparatorComponent={() => <View style={{height: 1, backgroundColor: 'gray'}} />} [OK]FlatList?
const data = ['Apple', 'Banana', 'Cherry'];
<FlatList
data={data}
renderItem={({item}) => <Text>{item}</Text>}
ItemSeparatorComponent={() => <View style={{height: 2, backgroundColor: 'black'}} />}
/>View with height 2 and black background, so a thin black line.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'}} />"
/>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?marginHorizontal: 20 to add space on left and right sides.Pass the test and this pattern moves unknown