Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

List Components


In React Native, list components are crucial for displaying collections of data efficiently and responsively. They allow developers to render large datasets while maintaining good performance by only rendering items that are currently visible on the screen. Here’s an overview of the key list components available:


1. FlatList

The FlatList component is the most commonly used list component in React Native. It efficiently renders large lists of data by only rendering the components that are currently visible on the screen. It uses a virtualized list approach, making it suitable for long lists where performance is critical.

Example

              <import React from 'react';>
              <import { FlatList, Text, StyleSheet } from 'react-native';>
              
              <const MyFlatList = () => {>
                <const data = [>
                  <{ id: '1', name: 'Item 1' },>
                  <{ id: '2', name: 'Item 2' },>
                  <{ id: '3', name: 'Item 3' },>
                  <// Add more data items as needed>
                <];>
              
                <const renderItem = ({ item }) => (>
                  <Text style={styles.item}>{item.name}</Text>
                <);>
              
                <return (>
                  <FlatList>
                    <data={data}>
                    <renderItem={renderItem}>
                    <keyExtractor={item => item.id}>
                  </FlatList>
                <);>
              <};>
              
              <const styles = StyleSheet.create({>
                <item: {>
                  <padding: 10,>
                  <fontSize: 18,>
                  <height: 44,>
                <},>
              <});>
              
              <export default MyFlatList;>
              

            

2.Section List

The SectionList component extends FlatList by allowing you to render sections with headers and footers. Each section can have its own data source, making it useful for categorizing data.

Example

                import React from 'react';
import { SectionList, Text, StyleSheet } from 'react-native';

const MySectionList = () => {
  const data = [
    {
      title: 'Group 1',
      data: [
        { id: '1', name: 'Item 1' },
        { id: '2', name: 'Item 2' },
      ],
    },
    {
      title: 'Group 2',
      data: [
        { id: '3', name: 'Item 3' },
        { id: '4', name: 'Item 4' },
      ],
    },
  ];

  const renderItem = ({ item }) => (
    <Text style={styles.item}>{item.name}</Text>
  );

  const renderSectionHeader = ({ section }) => (
    <Text style={styles.sectionHeader}>{section.title} </Text >
  );

  return (
    <SectionList
      sections={data}
      renderItem={renderItem}
      renderSectionHeader={renderSectionHeader}
      keyExtractor={item => item.id}
    >
  );
};

const styles = StyleSheet.create({
  sectionHeader: {
    fontSize: 24,
    backgroundColor: '#f0f0f0',
    padding: 10,
  },
  item: {
    padding: 10,
    fontSize: 18,
    height: 44,
  },
});

export default MySectionList;

            

Key Concepts

Data: Each list component receives data as a prop, which is an array of items to be rendered.

Rendering Item: Defined by renderItem, which specifies how each item in the data should be rendered.

Key Extraction:keyExtractor prop is used to extract a unique key for each item, improving list performance and helping React identify which items have changed, been added, or been removed.

These components are essential for creating dynamic and responsive user interfaces in React Native applications, allowing developers to efficiently manage and display data in various formats while maintaining optimal performance.



Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java