0Pricing
React Native Academy · Lekcja

SectionList z nagłówkami

Grupuj elementy w sekcje za pomocą SectionList, renderuj przyklejony nagłówek każdej grupy i dostosuj separator między elementami.

SectionList z nagłówkami to bezpłatna lekcja React Native Academy na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej React Native Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs React Native Academy zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

When to Use SectionList

SectionList is the React Native component for displaying grouped lists — data divided into sections, each with its own header. It is ideal for contacts lists sorted alphabetically, settings screens with grouped options, or e-commerce categories. Like FlatList, it virtualizes rendering for performance.

SectionList Data Format

SectionList expects its sections prop to be an array of section objects. Each section object must have a data array containing the items for that section. You also typically include a title or other metadata to render the section header.

const SECTIONS = [
  {
    title: 'A',
    data: [
      { id: '1', name: 'Alice' },
      { id: '2', name: 'Andrew' },
    ],
  },
  {
    title: 'B',
    data: [
      { id: '3', name: 'Bob' },
      { id: '4', name: 'Beth' },
    ],
  },
];

Rendering Items with renderItem

The renderItem function in SectionList receives an object with the current item, its index within the section, and the section object itself. This gives you access to both the item data and the enclosing section's metadata in the same render call.

const renderItem = ({ item, section }) => (
  <View style={styles.row}>
    <Text>{item.name}</Text>
    <Text style={styles.sectionLabel}>{section.title}</Text>
  </View>
);

Rendering Section Headers

Pass a renderSectionHeader function to display a header above each section. The function receives { section } — the current section object. Returning a View with a colored background makes the header stand out from the items below it.

const renderSectionHeader = ({ section }) => (
  <View style={styles.sectionHeader}>
    <Text style={styles.sectionTitle}>{section.title}</Text>
  </View>
);

<SectionList
  sections={SECTIONS}
  renderItem={renderItem}
  renderSectionHeader={renderSectionHeader}
  keyExtractor={(item) => item.id}
/>

Sticky Section Headers

By default, section headers scroll with the list. Set stickySectionHeadersEnabled to true to make headers stick to the top of the screen as you scroll past them — the same behavior you see in the iOS Contacts app. This is set to true by default on iOS and false on Android.

<SectionList
  sections={SECTIONS}
  renderItem={renderItem}
  renderSectionHeader={renderSectionHeader}
  keyExtractor={(item) => item.id}
  stickySectionHeadersEnabled={true}
/>

Section Footer with renderSectionFooter

Use renderSectionFooter to render content after the last item in each section. This is useful for showing a 'View all' link or a count of remaining items below a truncated group. The function receives the same { section } argument as renderSectionHeader.

const renderSectionFooter = ({ section }) => (
  <TouchableOpacity>
    <Text style={styles.viewAll}>
      View all {section.data.length} {section.title} contacts
    </Text>
  </TouchableOpacity>
);

Item Separators in SectionList

Like FlatList, SectionList supports ItemSeparatorComponent to render dividers between items within a section. SectionList also provides SectionSeparatorComponent to render a separator between the end of one section (or its footer) and the start of the next section's header.

const ItemSeparator = () => <View style={styles.separator} />;
const SectionSeparator = () => <View style={styles.sectionSeparator} />;

<SectionList
  sections={SECTIONS}
  renderItem={renderItem}
  renderSectionHeader={renderSectionHeader}
  keyExtractor={(item) => item.id}
  ItemSeparatorComponent={ItemSeparator}
  SectionSeparatorComponent={SectionSeparator}
/>

A Full SectionList Example

Bringing it all together: define your sections data, provide renderItem and renderSectionHeader, set a keyExtractor, and optionally enable sticky headers. This pattern covers the majority of grouped list use cases in production apps.

import { SectionList, View, Text, StyleSheet } from 'react-native';

export default function ContactsScreen() {
  return (
    <SectionList
      sections={SECTIONS}
      keyExtractor={(item) => item.id}
      renderItem={({ item }) => (
        <View style={styles.row}><Text>{item.name}</Text></View>
      )}
      renderSectionHeader={({ section }) => (
        <View style={styles.header}><Text style={styles.headerText}>{section.title}</Text></View>
      )}
      stickySectionHeadersEnabled
    />
  );
}

Building Data for SectionList from a Flat Array

Often your API returns a flat array. You need to group items by a common property before passing them to SectionList. Use Array.reduce to build a map, then convert it to the sections format. Sort the sections alphabetically for a predictable order.

function groupByFirstLetter(contacts) {
  const map = contacts.reduce((acc, contact) => {
    const letter = contact.name[0].toUpperCase();
    if (!acc[letter]) acc[letter] = [];
    acc[letter].push(contact);
    return acc;
  }, {});
  return Object.keys(map).sort().map((key) => ({
    title: key,
    data: map[key],
  }));
}

Scrolling to a Specific Section

Use a ref on the SectionList and call scrollToLocation to jump to a specific item or section header. This is essential for alphabetic index bars (like the sidebar in iOS Contacts) where tapping a letter scrolls the list to that section.

const sectionListRef = useRef(null);

const scrollToSection = (sectionIndex) => {
  sectionListRef.current?.scrollToLocation({
    sectionIndex,
    itemIndex: 0,
    animated: true,
  });
};

<SectionList ref={sectionListRef} sections={SECTIONS} ... />

Performance Tips for SectionList

The same performance rules that apply to FlatList apply to SectionList. Stabilize renderItem and renderSectionHeader with useCallback. Avoid creating new objects inline in the sections prop on every render — memoize the processed sections data with useMemo.

const sections = useMemo(
  () => groupByFirstLetter(contacts),
  [contacts]
);

const renderItem = useCallback(({ item }) => (
  <Row item={item} />
), []);

Quick Check

Test your understanding of SectionList with headers from this lesson.

Lesson Recap

In this lesson you learned: SectionList sections prop takes an array of objects each with a data array, renderSectionHeader renders a title above each group, and stickySectionHeadersEnabled pins headers to the top as users scroll. Next up we build a complete Contacts List App combining SectionList, search, and pull-to-refresh.

Często zadawane pytania

Czy lekcja „SectionList z nagłówkami” jest bezpłatna?

Tak — pełny tekst „SectionList z nagłówkami” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu React Native Academy, przejdź na CoddyKit PRO. Kurs React Native Academy zawiera 4 lekcji w sumie.

Co nauczysz się w „SectionList z nagłówkami”?

Grupuj elementy w sekcje za pomocą SectionList, renderuj przyklejony nagłówek każdej grupy i dostosuj separator między elementami. Ćwiczysz React Native Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć React Native Academy?

Nie wymagamy żadnego doświadczenia. React Native Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.

Ile czasu zajmuje lekcja „SectionList z nagłówkami”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji React Native Academy?

Tak. Każda lekcja React Native Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Dane FlatList, renderItem i keyExtractor
  2. Odświeżanie przez przeciągnięcie i ładowanie kolejnych danych
  3. SectionList z nagłówkami
  4. Tworzenie aplikacji z listą kontaktów
← Powrót do React Native Academy