The VirtualizedList component is best when your list is massively big in size.VirtualizedList helps in better performance and memory usage.As user scrolls, the data is shown to the user.
The basic component of VirtualizedList is as follows &minuns;
<VirtualizedList data={DATA} initialNumToRender={4} renderItem={renderItem} keyExtractor={keyExtractor} getItemCount={getItemCount} getItem={getItem} />
Important VirtualizedList Properties
Props | Description |
---|---|
renderItem | The items from the data will be rendered inside the VirtualizedList. |
data | The data to be displayed. |
getItem | Function that will fetch the individual item from the data. |
getItemCount | Gets the count of the data items. |
initialNumToRender | No of times to be rendered at the start. |
keyExtractor | The unique key to be considered for each item for the specified index. |
Here is a working example of VirtualizedList.
Example: Display of data using VirtualizedList
To work with VirtualizedList import it first as follows −
import { SafeAreaView, View, VirtualizedList, StyleSheet, Text } from 'react-native';
The code for VirtualizedList is as follows −
<SafeAreaView> <VirtualizedList data={DATA} initialNumToRender={4} renderItem={({ item }) => <Item title={item.title} />} keyExtractor={item => item.id} getItemCount={getItemCount} getItem={getItem} /> </SafeAreaView>
The initial items that we want to display to render is 4. The renderItem property is used to display the item on the screen. It makes use of custom Item component defined as shown below −
const Item = ({ title })=> { return ( <View style={styles.item}> <Text style={styles.title}>{title}</Text> </View> ); }
The keyExtractor defines the unique key for each item.
keyExtractor={item => item.id}
The props getItemCount gets the total count of items that will be displayed to the user. Right now it calls the function getItemCount that is defined as follows.
const getItemCount = (data) => { return 100; } getItemCount={getItemCount}
The getITem props is meant to get the data to be displayed.It calls the method getItem and it is defined as follows −
const getItem = (data, index) => { return { id: index, title: 'test' } } getItem={getItem}
The complete code to display VirtualizedList is as follows −
import React from 'react'; import { SafeAreaView, View, VirtualizedList, StyleSheet, Text } from 'react-native'; const DATA = []; const getItem = (data, index) => { return { id: index, title: 'test' } } const getItemCount = (data) => { return 100; } const Item = ({ title })=> { return ( <View style={styles.item}> <Text style={styles.title}>{title}</Text> </View> ); } const VirtualizedListExample = () => { return ( <SafeAreaView> <VirtualizedList data={DATA} initialNumToRender={4} renderItem={({ item }) => <Item title={item.title} />} keyExtractor={item => item.id} getItemCount={getItemCount} getItem={getItem} /> </SafeAreaView> ); } const styles = StyleSheet.create({ item: { backgroundColor: '#ccc', height: 100, justifyContent: 'center', marginVertical: 8, marginHorizontal: 16, padding: 20, }, title: { fontSize: 32, }, }); export default VirtualizedListExample;