Virtualized List

Learn via video courses
Topics Covered

Overview

In React Native, we have used SectionList and FlatList. But do you have any idea where they come from?

They are all derived from VirtualisedList React Native, which means both are virtualized list component-based encapsulations. We can even virtualize all properties in SectionList and FlatList as well.

Introduction

React Native VirtualisedList is a component that is used to render lists of data that are not all visible on the screen at the same time. This can be helpful to improve the performance of your app, as it only renders the parts of the list that are actually visible on the screen.

VirtualisedList React Native works by creating a virtual representation of the list. This virtual representation is a data structure that stores the positions of all the items in the list. When the user scrolls through the list, React Native VirtualisedList only renders the parts of the virtual representation that are visible on the screen.

concept of regular scrolling and virtualized scrolling

In React Native Scrollview, if we wanted to view a list of items, it would render at once for all components. But using the React Native VirtualisedList component, we can render a specific number of items at a time, which gives us great performance.

Example

We can use the following crypto data to view the list of crypto data using a React Native VirtualisedList.

Note: 📄Data files Use this data file to represent a list using VirtualisedList React Native

components/index.js

components/styles.js

This styling is used for the component/index.js file in VirtualisedList React Native.

src/index.js

In the following code, we are using a VirtualisedList React Native component and importing the CoinItem component from component/index.

App.js

This is the parent file where all the children's components go in React Native VirtualisedList.

Result

final output of using virtualizedlist

Props

In React Native VirtualisedList, there are very useful properties available to customise the styling and improve performance.

PropertyTypeDescription
dataArray or anypassed to getItem and getItemCount to retrieve items.
getItemfunctionreturns the item from the data array at a given index.
getItemCountfunctionreturns the number of items in the data array.
renderItemfunctionIt receives an item from the data array and returns a React element to be rendered in VirtualisedList React Native.
CellRendererComponentReact Componentallows customizing how cells rendered by renderItem/ListItemComponent are wrapped when placed into the underlying ScrollView.
ItemSeparatorComponentcomponent, function, elementRendered in between each item
ListEmptyComponentcomponent, elementRendered when the list is empty
ListItemComponentcomponent, functionEach data item is rendered using this element
ListFooterComponentcomponent, elementRendered at the bottom of all the items
ListFooterComponentStyleViewStylePropStyling for internal View for ListFooterComponent
ListHeaderComponentcomponent, elementRendered at the top of all the items
ListHeaderComponentStyleViewStylePropStyling for internal View for ListHeaderComponent
debugBooleanenables additional logging and visual overlays to help with use and implementation debugging.
extraDataanyif you have a list of items that are loaded from an API, you could use the extraData prop to store the current page number
getItemLayoutfunctionprovides the height and offset for an item in the list
horizontalBooleanrenders items next to each other horizontally
initialNumToRenderNumberThe number of items to render initially.
initialScrollIndexNumberThe initial index of the item to be displayed.
invertedBooleanReverses the direction of scroll
listKeyStringA unique identifier for this list. If there are multiple VirtualizedLists at the same level of nesting within another VirtualizedList,
keyExtractorFunctionused to extract a unique key for each item in the data array
maxToRenderPerBatchNumberThe maximum number of items to render in each incremental render batch
onEndReachedFunctionwhen the end of the list is reached
onEndReachedThresholdNumbertrigger a callback function in the end of the list
onRefreshFunctionhandle the "pull-to-refresh" functionality.
onScrollToIndexFailedFunctionhandle failures when scrolling to an index that has not been measured
onStartReachedFunctionwhen the scroll position gets within the onStartReachedThreshold of the logical start of the list.
onStartReachedThresholdNumbertrigger a callback function for the initial scroll of the list
onViewableItemsChangedFunctioncalled when the list of viewable items changes.
persistentScrollbarBooleanspecifies whether or not the scrollbar should be visible when the list is not scrolling.
progressViewOffsetNumberSet this when an offset is needed for the loading indicator to show correctly.
refreshControlelementOnly works for vertical VirtualizedList.
refreshingBooleanwhether the list is currently being refreshed.
removeClippedSubviewsBooleanwhether offscreen items should be removed from the native view hierarchy to improve performance.
renderScrollComponentFunctioncustom RefreshControl like component
viewabilityConfigObjectwhich items are considered "viewable" in the list.
viewabilityConfigCallbackPairsArrayList of ViewabilityConfig pairs
updateCellsBatchingPeriodNumberspecifies the amount of time between low-priority item render batches.
windowSizeNumberrendered outside the visible area in both directions. This helps in quickly rendering when the user scrolls in React Native VirtualisedList

Methods

a. flashScrollIndicators()

React Native VirtualisedList is used to temporarily display the scroll indicators. This can be useful for debugging or for temporarily highlighting the scroll position.

b. getScrollableNode()

useful for performing custom scrolling operations or getting the dimensions of the scrollable area.

c. getScrollRef()

The working functionality is the same as getScrollableNode, but the returned value is a React.ElementRef<typeof ScrollView> or React.ElementRef<typeof View>

d. getScrollResponder()

The getScrollResponder() method is used to get a handle on the underlying scroll responder. The scroll responder can then be used to listen to scroll events or perform custom scrolling operations in React Native VirtualisedList.

e. scrollToEnd()

used to scroll to the end of the list after the data had changed. For example, if you add more items to the list, you can use the scrollToEnd() method to scroll to the new items.

f. scrollToIndex()

focusing on a particular item or making sure that a specific item is visible.

g. scrollToItem()

The scrollToIndex() method takes an index parameter, while the scrollToItem() method takes an item parameter. The scrollToIndex() method scrolls to the specified index, while the scrollToItem() method scrolls to the specified item, regardless of its index.

h. scrollToOffset()

Scroll to a specific offset in the list. The offset parameter specifies the offset from the top of the list that you want to scroll to. The animated parameter specifies whether or not the scroll should be animated in React Native VirtualisedList

Conclusion

  • It does this by only rendering the items that are currently visible on the screen and by caching the rendered items so that they do not need to be re-rendered when they are scrolled into view.
  • Can we make a custom connection using React Native VirtualisedList?
    Yes, we can make a custom component using VirtualizedList. This can be useful for creating custom list layouts or adding custom functionality to the list.
  • React Native VirtualisedList could be used to create more efficient list components, such as lists that are optimised for performance on low-end devices.