To create a list view in React Native using SectionList, first import the SectionList component in code from the React Native library.
  1. import { SectionList } from 'react-native';
Properties
  1. Sections: Contain actual data to render on the screen.
  2. renderItem: To display the items inside the SectionList.
  3. renderSectionHeader: To show the section header title on each section.
  4. keyExtractor: To extract the unique key for a given item when SectionList render.
Code
  1. import React, { Component } from "react";
  2. import { SectionList, Text, StyleSheet, View } from 'react-native';
  3. const Regions = [
  4. { title: 'Asia', data: ['India', 'Bangladesh', 'Bhutan', 'China', 'Japan'] },
  5. { title: 'Europe', data: ['Denmark', 'France', 'Germany', 'Italy'] },
  6. { title: 'North America', data: ['Canada', 'Mexico', 'The United States of America'] }
  7. ];
  8. class App extends Component {
  9. render() {
  10. return (
  11. <View >
  12. <SectionList
  13. sections={Regions}
  14. renderItem={({ item }) => <Text style={styles.ItemStyle}>{item}</Text>}
  15. renderSectionHeader={({ section }) => <Text style={styles.HeaderStyle}>{section.title}</Text>}
  16. keyExtractor={(item, index) => index}
  17. />
  18. </View>
  19. );
  20. }
  21. }
  22. const styles = StyleSheet.create({
  23. HeaderStyle: {
  24. backgroundColor: '#1A237E',
  25. fontSize: 20,
  26. padding: 5,
  27. color: "yellow",
  28. borderRadius: 10,
  29. textAlign: 'center',
  30. },
  31. ItemStyle: {
  32. padding: 5,
  33. color: '#fff',
  34. backgroundColor: '#B08395',
  35. fontStyle: 'italic',
  36. fontFamily: "French Script MT",
  37. borderWidth: 1,
  38. borderColor: '#d6d7da',
  39. fontSize: 20,
  40. paddingLeft: 20
  41. }
  42. });
  43. export default App;
Output
Summary
The SectionList component is very easy to use in React Native to create a list view. In my next blog, I will talk more about the SectionList component. Hopefully this helped!