Build: Grouped Contacts
Display a list of contacts grouped by the first letter of their names using SectionList.
Target UI
Grouped Contacts A - Alice - Aaron B - Bob - Brenda C - Charlie - Clara
Grouped Contacts A - Alice - Aaron B - Bob - Brenda C - Charlie - Clara
import React from 'react'; import { View, Text, SectionList, StyleSheet } from 'react-native'; export default function GroupedContacts() { const sections = [ // TODO: Add grouped data here ]; return ( <View style={styles.container}> <SectionList sections={sections} keyExtractor={(item, index) => item + index} renderItem={({ item }) => ( // TODO: Render each contact name here )} renderSectionHeader={({ section: { title } }) => ( // TODO: Render section header here )} /> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, marginTop: 40, paddingHorizontal: 20 }, header: { fontSize: 24, fontWeight: 'bold', backgroundColor: '#eee', paddingVertical: 5 }, item: { fontSize: 18, paddingVertical: 5 } });
import React from 'react'; import { View, Text, SectionList, StyleSheet } from 'react-native'; export default function GroupedContacts() { const sections = [ { title: 'A', data: ['Alice', 'Aaron'] }, { title: 'B', data: ['Bob', 'Brenda'] }, { title: 'C', data: ['Charlie', 'Clara'] } ]; return ( <View style={styles.container}> <SectionList sections={sections} keyExtractor={(item, index) => item + index} renderItem={({ item }) => <Text style={styles.item}>{item}</Text>} renderSectionHeader={({ section: { title } }) => ( <Text style={styles.header}>{title}</Text> )} /> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, marginTop: 40, paddingHorizontal: 20 }, header: { fontSize: 24, fontWeight: 'bold', backgroundColor: '#eee', paddingVertical: 5 }, item: { fontSize: 18, paddingVertical: 5 } });
This app uses React Native's SectionList to display contacts grouped by their first letter.
The sections array contains objects with a title for the group header and a data array for the contacts in that group.
The renderSectionHeader shows the group letter in bold with a light background.
The renderItem shows each contact name with some padding.
This structure makes it easy to show grouped lists like contacts or categories.
Grouped Contacts A Alice Aaron B Bob Brenda C Charlie Clara
SectionList in React Native?SectionList is designed to show lists grouped by sections, each with a header and items.sections prop for a SectionList?title and a data array of items.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>}
/>renderSectionHeader renders the title of each section as header.SectionList usage?
const sections = [
{ title: 'Numbers', data: 12345 }
];
<SectionList
sections={sections}
renderItem={({ item }) => <Text>{item}</Text>}
renderSectionHeader={({ section }) => <Text>{section.title}</Text>}
/>data property must be an array of items, but here it is a number (12345).SectionList expects an array to iterate over.SectionList with sections for weekdays and weekend, but only show days that start with 'S'. How should you prepare the sections data?filter(day => day.startsWith('S')) on weekdays and weekend arrays to keep only days starting with 'S'.Pass the test and this pattern moves unknown