📌  相关文章
📜  react native flatlist - Javascript(1)

📅  最后修改于: 2023-12-03 15:34:38.565000             🧑  作者: Mango

React Native FlatList

Introduction

React Native FlatList is a component that allows you to create a list of items efficiently. It renders only the visible items, which improves performance and reduces memory usage. It is a commonly used component in React Native.

Features

React Native FlatList has several features that make it a powerful tool for displaying large lists. Some of its key features include:

  • Lazy loading of items: FlatList only renders the items that are currently visible on the screen. Additional items are loaded as the user scrolls down the list.
  • Optimized rendering: FlatList uses optimizations such as recycling views and only rendering items that have changed since the last render.
  • Horizontal and vertical scrolling: You can configure FlatList to scroll horizontally or vertically depending on your use case.
  • Pull-to-refresh: FlatList can be configured to support a pull-to-refresh gesture, which allows the user to refresh the contents of the list by pulling down on the screen.
  • Item separators: You can add separators between items in the list to improve readability.
Example
import React from 'react';
import { FlatList, Text, View } from 'react-native';

const data = [
  { key: 'Apple' },
  { key: 'Banana' },
  { key: 'Cherry' },
  { key: 'Durian' },
  { key: 'Elderberry' },
  { key: 'Fig' },
  { key: 'Grape' },
  { key: 'Honeyberry' },
  { key: 'Iced melon' },
  { key: 'Jackfruit' },
  { key: 'Kiwi' },
  { key: 'Lemon' },
  { key: 'Mango' },
  { key: 'Nectarine' },
];

const Item = ({ title }) => (
  <View style={{ backgroundColor: '#fff' }}>
    <Text style={{ fontSize: 24 }}>{title}</Text>
  </View>
);

const FlatListExample = () => {
  const renderItem = ({ item }) => (
    <Item title={item.key} />
  );

  return (
    <FlatList
      data={data}
      renderItem={renderItem}
      ItemSeparatorComponent={() => <View style={{ height: 1, backgroundColor: '#f2f2f2' }} />}
    />
  );
};

export default FlatListExample;
Conclusion

React Native FlatList is a powerful tool for displaying large lists efficiently and with minimal memory usage. It is easy to configure and has several features that make it a versatile component in your React Native application.