A SectionList helps you show lists that have groups or categories. It makes it easy to see items sorted by sections.
SectionList for grouped data in React Native
import { SectionList, Text, View } from 'react-native'; const DATA = [ { title: 'Group Title', data: ['item1', 'item2'] }, { title: 'Another Group', data: ['item3', 'item4'] } ]; export default function App() { return ( <SectionList sections={DATA} keyExtractor={(item, index) => item + index} renderItem={({ item }) => <Text>{item}</Text>} renderSectionHeader={({ section }) => <Text>{section.title}</Text>} /> ); }
The sections prop takes an array of objects, each with a title and a data array.
renderItem shows each item, and renderSectionHeader shows the group title.
const DATA = [];
// Empty list example
<SectionList sections={DATA} renderItem={() => null} renderSectionHeader={() => null} />const DATA = [
{ title: 'Fruits', data: ['Apple'] }
];
// One group with one item
<SectionList sections={DATA} renderItem={({ item }) => <Text>{item}</Text>} renderSectionHeader={({ section }) => <Text>{section.title}</Text>} />const DATA = [
{ title: 'Fruits', data: ['Apple', 'Banana'] },
{ title: 'Vegetables', data: ['Carrot', 'Peas'] }
];
// Multiple groups with multiple items
<SectionList sections={DATA} renderItem={({ item }) => <Text>{item}</Text>} renderSectionHeader={({ section }) => <Text>{section.title}</Text>} />This app shows a list with two groups: Fruits and Vegetables. Each group has items listed below its title.
import React from 'react'; import { SectionList, Text, View, StyleSheet } from 'react-native'; const DATA = [ { title: 'Fruits', data: ['Apple', 'Banana', 'Orange'] }, { title: 'Vegetables', data: ['Carrot', 'Peas', 'Broccoli'] } ]; export default function App() { return ( <View style={styles.container}> <SectionList sections={DATA} keyExtractor={(item, index) => item + index} renderItem={({ item }) => <Text style={styles.item}>{item}</Text>} renderSectionHeader={({ section }) => <Text style={styles.header}>{section.title}</Text>} /> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, paddingTop: 40, paddingHorizontal: 20 }, header: { fontSize: 24, fontWeight: 'bold', backgroundColor: '#eee', paddingVertical: 5 }, item: { fontSize: 18, paddingVertical: 3, paddingLeft: 10 } });
SectionList is efficient for large grouped lists because it only renders visible items.
Time complexity depends on the number of items, but SectionList optimizes rendering.
Common mistake: forgetting to provide a unique keyExtractor for items can cause warnings.
Use SectionList instead of FlatList when you want to group items under headers.
SectionList shows lists grouped by sections with headers.
Each section has a title and an array of items.
Use renderItem for items and renderSectionHeader for group titles.
Practice
SectionList in React Native?Solution
Step 1: Understand SectionList usage
SectionListis designed to show lists grouped by sections, each with a header and items.Step 2: Compare options with SectionList purpose
Options B, C, and D describe other UI or functionality unrelated to grouped lists.Final Answer:
To display a list of items grouped by sections with headers -> Option AQuick Check:
SectionList = grouped list display [OK]
- Confusing SectionList with FlatList
- Thinking SectionList is for navigation
- Assuming SectionList creates grids
sections prop for a SectionList?Solution
Step 1: Recall SectionList sections structure
Each section is an object with atitleand adataarray of items.Step 2: Check options for correct keys and structure
[{ title: 'Fruits', data: ['Apple', 'Banana'] }, { title: 'Veggies', data: ['Carrot'] }] matches the required format. Options B, C, and D use wrong keys or structure.Final Answer:
[{ title: 'Fruits', data: ['Apple', 'Banana'] }, { title: 'Veggies', data: ['Carrot'] }] -> Option AQuick Check:
sections = array of {title, data} objects [OK]
- Using wrong keys like 'items' or 'header'
- Passing a flat array instead of array of objects
- Confusing data array with section title
const sections = [
{ title: 'A', data: ['Apple', 'Apricot'] },
{ title: 'B', data: ['Banana'] }
];
<SectionList
sections={sections}
renderItem={({ item }) => <Text>{item}</Text>}
renderSectionHeader={({ section }) => <Text>{section.title}</Text>}
/>Solution
Step 1: Understand renderSectionHeader usage
TherenderSectionHeaderrenders thetitleof each section as header.Step 2: Identify section titles in data
Sections have titles 'A' and 'B', so these will be shown as headers above their items.Final Answer:
A and B as headers above their items -> Option DQuick Check:
Section titles = headers [OK]
- Thinking items become headers
- Assuming only first section shows header
- Forgetting to use renderSectionHeader
SectionList usage?
const sections = [
{ title: 'Numbers', data: 12345 }
];
<SectionList
sections={sections}
renderItem={({ item }) => <Text>{item}</Text>}
renderSectionHeader={({ section }) => <Text>{section.title}</Text>}
/>Solution
Step 1: Check the type of data property
Thedataproperty must be an array of items, but here it is a number (12345).Step 2: Understand consequences of wrong data type
This will cause runtime errors becauseSectionListexpects an array to iterate over.Final Answer:
The data property should be an array, not a number -> Option BQuick Check:
data must be array [OK]
- Passing a single value instead of array to data
- Confusing title type with data type
- Using wrong render method names
SectionList with sections for weekdays and weekend, but only show days that start with 'S'. How should you prepare the sections data?Solution
Step 1: Filter days starting with 'S' in each group
Usefilter(day => day.startsWith('S'))on weekdays and weekend arrays to keep only days starting with 'S'.Step 2: Assign filtered arrays to correct section titles
Weekdays filtered will be empty, weekend filtered will have 'Saturday' and 'Sunday'. This matches the requirement.Final Answer:
[{ title: 'Weekdays', data: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'].filter(day => day.startsWith('S')) }, { title: 'Weekend', data: ['Saturday', 'Sunday'].filter(day => day.startsWith('S')) }] -> Option CQuick Check:
Filter each section's data by startsWith('S') [OK]
- Not filtering weekdays, showing all days
- Combining all days into one section
- Ignoring filtering and passing raw arrays
Ready to prove it?
Pass the test and this pattern moves unknown
