Home > Web Front-end > JS Tutorial > body text

Explain the usage of VirtualizedList component in ReactNative?

王林
Release: 2023-08-24 13:45:02
forward
1542 people have browsed it

The VirtualizedList component is best when your list is very large. VirtualizedList helps improve performance and memory usage. As the user scrolls, the data is displayed to the user.

The basic components of VirtualizedList are as follows&minuns;

<VirtualizedList data={DATA} initialNumToRender={4} renderItem={renderItem} keyExtractor={keyExtractor} getItemCount={getItemCount} getItem={getItem} />
Copy after login

Important VirtualizedList properties

PropertiesDescription
renderItem will render the item in the data Within VirtualizedList.
dataThe data to be displayed.
getItemFunction to get a single item
getItemCountGet the number of data items.
initialNumToRender< /td>The number of times to render at the beginning.
keyExtractorEach unique key to be considered The item for the specified index.

This is a working example of VirtualizedList.

Example: Using VirtualizedList to display data

To use VirtualizedList, first import it as follows-

import { SafeAreaView, View, VirtualizedList, StyleSheet, Text } from &#39;react-native&#39;;
Copy after login

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>
Copy after login

us The initial item to display rendering is 4. The renderItem property is used to display the item on the screen. It uses a custom Item component defined as follows -

const Item = ({ title })=> {
   return (
      <View style={styles.item}>
      <Text style={styles.title}>{title}</Text>
      </View>
   );
}
Copy after login

keyExtractor to define a unique key for each item.

keyExtractor={item => item.id}
Copy after login

props getItemCount Gets the total number of items that will be displayed to the user. Now it calls the function getItemCount, which is defined as follows.

const getItemCount = (data) => {
   return 100;
}
getItemCount={getItemCount}
Copy after login

getITem attribute is used to get the data to be displayed. It calls the getItem method, which is defined as follows -

const getItem = (data, index) => {
   return {
      id: index,
      title: &#39;test&#39;
   }
}
getItem={getItem}
Copy after login

The complete code to display VirtualizedList is as follows -

import React from 'react';
import { SafeAreaView, View, VirtualizedList, StyleSheet, Text } from &#39;react-native&#39;;
const DATA = [];
const getItem = (data, index) => {
   return {
      id: index,
      title: 'test'
   }
}
const getItemCount = (data) => {
   return 100;
}
const Item = ({ title })=> {
   return (
      
         {title}
         
      );
   }
   const VirtualizedListExample = () => {
      return (
         
             }
               keyExtractor={item => item.id}
               getItemCount={getItemCount}
               getItem={getItem}
         />
         
      );
   }
   const styles = StyleSheet.create({
   item: {
      backgroundColor: '#ccc',
      height: 100,
      justifyContent: 'center',
      marginVertical: 8,
      marginHorizontal: 16,
      padding: 20,
   },
   title: {
      fontSize: 32,
   },
});
export default VirtualizedListExample;
Copy after login

Output

解释一下 ReactNative 中 VirtualizedList 组件的用法?

The above is the detailed content of Explain the usage of VirtualizedList component in ReactNative?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!