What is the use of FlatList?
What is the use of FlatList?
The FlatList
component in React Native is used to efficiently render large lists of data in a scrollable view. It is designed to handle dynamic and potentially large datasets by only rendering the items that are currently visible on the screen, which optimizes performance and memory usage.
Efficient Rendering:
FlatList
only renders the items that are currently visible on the screen, rather than all items at once. This lazy loading approach helps in managing memory and improving performance, especially for long lists of data[4][6].Required Props:
data
: An array of items to be rendered.renderItem
: A function that takes an item from the data
array and returns a formatted component to render[1][4][6].Optional Props:
keyExtractor
: A function to extract a unique key for each item, which helps in efficient re-rendering.ItemSeparatorComponent
: A component to render between each item.ListHeaderComponent
and ListFooterComponent
: Components to render at the top and bottom of the list, respectively.horizontal
: A boolean to render the list horizontally instead of vertically.numColumns
: A prop to render multiple columns[1][6][8].Performance Optimizations:
FlatList
supports various performance optimizations such as removeClippedSubviews
, getItemLayout
, and maxToRenderPerBatch
to further enhance the rendering efficiency[9].Use Cases:
FlatList
is ideal for scenarios where you need to display a large number of items, such as a list of contacts, messages, or any other data that can grow dynamically. It is also suitable for implementing infinite scrolling and pull-to-refresh functionalities[1][3][6].import React from 'react';
import { FlatList, Text, View, StyleSheet } from 'react-native';
const DATA = [
{ id: '1', title: 'Item 1' },
{ id: '2', t...
middle
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào