When building mobile apps, displaying long lists of items, like a feed of posts or a long menu, can severely impact performance if not handled correctly. Simply mapping over an array with ScrollView renders every single item, even those off-screen, consuming excessive memory and CPU. React Native's FlatList is designed to solve this exact problem. It's a "virtualized" list component, meaning it only renders items that are currently visible on screen, plus a small buffer above and below. As the user scrolls, FlatList efficiently recycles and re-renders only the necessary items, leading to significantly smoother scrolling and reduced memory footprint, crucial for a native-like user experience.
FlatList requires three primary props: data (your array of items), renderItem (a function that takes an item and returns the JSX to render for it), and keyExtractor (a function to extract a unique, stable key for each item, vital for React's reconciliation process and optimal performance). For lists where items are grouped into distinct sections, like a contacts list with A-Z headers or a menu categorized by food type, SectionList is your go-to. It builds upon FlatList's virtualization but adds robust support for section headers and footers. SectionList uses a sections prop (an array of objects, each with a title and a data array) and renderSectionHeader to display these groupings effectively.
The choice between FlatList and SectionList boils down to your data structure: FlatList for a single, flat array of items, and SectionList for grouped, sectional data. Both components come with advanced props for handling features like pull-to-refresh, infinite scrolling, empty list states, and custom separators, making them highly versatile. Mastering these components is fundamental for building performant and scalable React Native applications, ensuring your users enjoy a fluid and responsive experience regardless of the list's length. Always prefer FlatList or SectionList over ScrollView for lists that might contain many items to leverage their built-in performance optimizations.
Key Takeaways
FlatListandSectionListuse virtualization to render only visible items, significantly improving performance for long lists.- Choose
FlatListfor displaying a simple, flat array of data. - Opt for
SectionListwhen your data is grouped into distinct sections with headers. - Essential
FlatListprops:data,renderItem,keyExtractor(for uniqueness and performance). - Always prefer these components over
ScrollViewfor lists that may grow large to ensure a smooth user experience.
Code Example
How this code works
This code creates a highly efficient, scrollable list of items using React Native's FlatList component. Its primary job is to take an array of data and render each piece of data as a visually distinct item within a long, scrollable view, optimized for performance, especially with many items. The FlatList component itself handles all the complex logic for rendering only what's visible on screen, rather than attempting to render every item at once.
To achieve this, the FlatList relies on a few key props. The data prop receives the array of objects (like the data variable) that will be displayed in the list. The renderItem prop is a function that tells FlatList how to draw each individual item; it receives an object containing the current item from the data array, which is then styled using View and Text components. Crucially, keyExtractor is provided to give each item a unique identifier, usually item.id. This is vital for React to efficiently track items when they are added, removed, or reordered, significantly improving performance and preventing rendering bugs that might otherwise occur in long lists.