Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between FlatList and VirtualizedList

Tags:

react-native

I am new in react native, and am confused about the difference between FlatList and VirtualizedList.

So,

  1. What are the differences between FlatList and VirtualizedList ?
  2. When should I use each ?
like image 341
theapache64 Avatar asked Jun 19 '18 04:06

theapache64


People also ask

What is difference between FlatList and SectionList?

SectionList s are like FlatList s, but they can have section headers to separate groups of rows. SectionList s render each item in their input sections using the renderSectionHeader and renderItem prop. Each item in sections should be an object with a unique id (the key), and an array data of row data.

What is VirtualizedList?

Base implementation for the more convenient <FlatList> and <SectionList> components, which are also better documented. In general, this should only really be used if you need more flexibility than FlatList provides, e.g. for use with immutable data instead of plain arrays.

What is the difference between MAP and FlatList in React Native?

Displaying data using map or other loop methods may give you flexibility to customize the list the way you want, but as the amount of data grows, you also need to take extra care of the bulk data you render on the visible screen. FlatList makes handling complex and large amounts of list data easy and effortless.


1 Answers

The <FlatList> is a performant interface for rendering basic, flat lists.

On the other side, the <VirtualizedList> is a base implementation of the <FlatList> and <SectionList> components, which are also better documented. In general, <VirtualizedList> should only really be used if you need more flexibility than FlatList provides, e.g. for use with immutable data instead of plain arrays.

FlatList example:

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

  return (
    <SafeAreaView style={styles.container}>
      <FlatList
        data={DATA}
        renderItem={renderItem}
        keyExtractor={item => item.id}
      />
    </SafeAreaView>
  );
}

VirtualizedList example:

const App = () => {
  return (
    <SafeAreaView style={styles.container}>
      <VirtualizedList
        data={DATA}
        initialNumToRender={4}
        renderItem={({ item }) => <Item title={item.title} />}
        keyExtractor={item => item.key}
        getItemCount={getItemCount}
        getItem={getItem}
      />
    </SafeAreaView>
  );
}


More info:

https://reactnative.dev/docs/virtualizedlist

https://reactnative.dev/docs/flatlist

like image 194
Frederiko Cesar Avatar answered Sep 21 '22 17:09

Frederiko Cesar