Зміст курсу
Foundations of React Native
Foundations of React Native
Rendering Data Lists
Theory
FlatList
and SectionList
are components in React Native used for rendering lists of data. Both components are efficient for handling large lists and are optimized for performance. The key difference lies in how they handle data and their use cases.
FlatList
Why Use FlatList
- Dynamic Data Rendering: Ideal for rendering large sets of dynamic data efficiently. Renders only the items that are currently visible on the screen, ensuring performance;
- Memory Efficiency: Memory-efficient as it doesn't load the entire dataset into memory at once;
- Lazy Loading: Implements lazy loading, meaning it only loads the data needed for the visible items.
Working with FlatList
data
: An array of data to be rendered;renderItem
: A function that renders an individual item;keyExtractor
: A function to extract a unique key for each item;onEndReached
(optional): A callback function that is called when the end of the list is reached;onRefresh
(optional): A callback function that is called when the user pulls the list down to refresh it.
Example
Code Description
-
Line 2: Import
FlatList
fromreact-native
. -
Lines 15-25:
FlatList
Renders a flat list with data, using the providedrenderItem
function and other props for customization.-
data
: Specifies the array of data that theFlatList
will render. In this case, it's the data array containing company information. -
renderItem
: Specifies a function that takes an item from the data array and renders it. In this case, it's rendering aView
with nestedText
components for the company's name and address. -
keyExtractor
: Specifies a function to extract a unique key for each item in the list. This is crucial for efficient rendering and updating of the list. In this case, it uses thekey
property from each item.
-
Result
SectionList
Why Use SectionList
- Organized Data: Useful when the data naturally falls into sections (e.g., categories, dates). Each section can have its own header;
- Improved Readability: Provides a structured and organized way to present data, improving readability;
- Dynamic Sections: Allows dynamic updates to both sections and data, making it suitable for dynamic content;
- Customizable Headers: Supports custom section headers through the
renderSectionHeader
prop.
Working with SectionList
sections
: An array of sections, where each section has a data array and optional renderItem and renderSectionHeader functions;renderItem
: A function that renders an individual item;renderSectionHeader
: A function that renders the section header;keyExtractor
: A function to extract a unique key for each item;onEndReached
(optional): A callback function that is called when the end of the list is reached;onRefresh
(optional): A callback function that is called when the user pulls the list down to refresh it.
Example
Code Description
-
Line 2: Import
SectionList
fromreact-native
. -
Lines 19-30:
SectionList
Renders a sectioned list with headers and items.-
sections
: Specifies an array of sections where each section has atitle
anddata
property. In this case, it's thesectionsData
array containing information about companies and employers. -
keyExtractor
: Specifies a function to extract a unique key for each item in the list. Similar toFlatList
, this is crucial for efficient rendering and updating of the list. -
renderItem
: Specifies a function that takes an item from thedata
array within each section and renders it. In this case, it's rendering aView
with aText
component for each item. -
renderSectionHeader
: Specifies a function that takes a section and renders its header. In this case, it's rendering aText
component for the section title.
-
Result
Both
FlatList
and SectionList
render sets of data. Choose between them based on your specific use case: use FlatList
for a simple flat list and SectionList
when you need to organize data into sections with headers.
In Practice
Все було зрозуміло?
Секція 2. Розділ 9
Зміст курсу
Foundations of React Native
Foundations of React Native
Rendering Data Lists
Theory
FlatList
and SectionList
are components in React Native used for rendering lists of data. Both components are efficient for handling large lists and are optimized for performance. The key difference lies in how they handle data and their use cases.
FlatList
Why Use FlatList
- Dynamic Data Rendering: Ideal for rendering large sets of dynamic data efficiently. Renders only the items that are currently visible on the screen, ensuring performance;
- Memory Efficiency: Memory-efficient as it doesn't load the entire dataset into memory at once;
- Lazy Loading: Implements lazy loading, meaning it only loads the data needed for the visible items.
Working with FlatList
data
: An array of data to be rendered;renderItem
: A function that renders an individual item;keyExtractor
: A function to extract a unique key for each item;onEndReached
(optional): A callback function that is called when the end of the list is reached;onRefresh
(optional): A callback function that is called when the user pulls the list down to refresh it.
Example
Code Description
-
Line 2: Import
FlatList
fromreact-native
. -
Lines 15-25:
FlatList
Renders a flat list with data, using the providedrenderItem
function and other props for customization.-
data
: Specifies the array of data that theFlatList
will render. In this case, it's the data array containing company information. -
renderItem
: Specifies a function that takes an item from the data array and renders it. In this case, it's rendering aView
with nestedText
components for the company's name and address. -
keyExtractor
: Specifies a function to extract a unique key for each item in the list. This is crucial for efficient rendering and updating of the list. In this case, it uses thekey
property from each item.
-
Result
SectionList
Why Use SectionList
- Organized Data: Useful when the data naturally falls into sections (e.g., categories, dates). Each section can have its own header;
- Improved Readability: Provides a structured and organized way to present data, improving readability;
- Dynamic Sections: Allows dynamic updates to both sections and data, making it suitable for dynamic content;
- Customizable Headers: Supports custom section headers through the
renderSectionHeader
prop.
Working with SectionList
sections
: An array of sections, where each section has a data array and optional renderItem and renderSectionHeader functions;renderItem
: A function that renders an individual item;renderSectionHeader
: A function that renders the section header;keyExtractor
: A function to extract a unique key for each item;onEndReached
(optional): A callback function that is called when the end of the list is reached;onRefresh
(optional): A callback function that is called when the user pulls the list down to refresh it.
Example
Code Description
-
Line 2: Import
SectionList
fromreact-native
. -
Lines 19-30:
SectionList
Renders a sectioned list with headers and items.-
sections
: Specifies an array of sections where each section has atitle
anddata
property. In this case, it's thesectionsData
array containing information about companies and employers. -
keyExtractor
: Specifies a function to extract a unique key for each item in the list. Similar toFlatList
, this is crucial for efficient rendering and updating of the list. -
renderItem
: Specifies a function that takes an item from thedata
array within each section and renders it. In this case, it's rendering aView
with aText
component for each item. -
renderSectionHeader
: Specifies a function that takes a section and renders its header. In this case, it's rendering aText
component for the section title.
-
Result
Both
FlatList
and SectionList
render sets of data. Choose between them based on your specific use case: use FlatList
for a simple flat list and SectionList
when you need to organize data into sections with headers.
In Practice
Все було зрозуміло?
Секція 2. Розділ 9