Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Display a List of Items in React Native with FlatList

    Chris AchardChris Achard
    react-nativeReact Native
    javascriptJavaScript
    androidAndroid
    iosiOS

    Using ScrollView is an easy way to scroll a list, but it isn’t the most memory efficient, which can really matter for performance on devices with limited memory. We’ll switch from using ScrollView to using FlatList, which will reuse views, and is the preferred way of showing long scrollable lists.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: We have a scrolling list of restaurant, but we're using a scroll view to show them. Instead, let's import FlatList from React native. Scroll view loads the entire view into memory when it mounts.

    Let's find for small number of items like we have here, but will cause memory and performance problems, if we're showing hundreds or thousands of items at a time. FlatList, however, is designed to be inaudible even with thousands of items, because it recycles the views which lowers the memory footprint.

    Unlike scroll view, FlatList takes a render prop for each item. Pass the filtered restaurant list into the FlatList as a prop name data. The render prop for each item is called render item.

    In the scroll view, we have each item rendered directly inline here. Let's clean that up a bit and first extract it into a new component file called restaurant row.js. There, we can import React's View, Text, and StyleSheet from React native and make our default component.

    Then, we can copy the row component and the styles as well. In this restaurant row, we use the place and the index, so we'll need to pass those in this props. Like an app.js, we can import the restaurant row and use it in the render item render prop.

    Render item takes a function and that function will get both the item and the index of the row that is being rendered. You can make a new function with item and index, and return a new restaurant row, passing in the list item as the place prop and the index as the index prop.

    As a last prop to FlatList, we have to provide a unique key for every row in the list. To set a prop called key extractor which is a function that will be passed in each item from the data.

    We need to return something unique for every row here. In our example, that will be the item's name. Now in practice, this is often a unique ID of some kind. Finally, we need to clean up this unused scroll view. Then, we can reload and see our FlatList.

    With this list, you can actually see how progressively loads the items. If I reload this simulator and pass the screen right as it loads, you can see it only loads the first 10 items and loads the rest later.

    This is part of the memory and performance saving techniques that are built in the FlatList. If we don't want that flicker of just the first 10 items loading now, we can specify a higher number.

    Let's pass an initial num to render prop to FlatList, and when we reload, there's no flicker.