Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native FlatList not scrolling to end

I've got (what I thought was) a simple FlatList which renders a list of Cards (code below)

Problem: the list renders, but won't scroll to fully display the last element in the list, OR to the content below the FlatList

What I've tried: basically everything in related SO questions:

  • Removing ALL styling
  • Wrapping the FlatList in a View or a ScrollView or both
  • Adding style={{flex: 1}} to the FlatList or wrappers (this causes **ALL* content to disappear)

Any ideas?

<FlatList
        data={props.filteredProducts}
        renderItem={({item}) => (
          <TouchableOpacity onPress={() => props.addItemToCart(item)}>
            <Card
              featuredTitle={item.key}
              image={require('../assets/icon.png')}
            />
          </TouchableOpacity>
        )}
        keyExtractor={item => item.key}
        ListHeaderComponent={
          <SearchBar />
        }
      />
...
<Other Stuff>
like image 955
rubie Avatar asked Mar 22 '19 11:03

rubie


People also ask

How do you scroll to end in FlatList?

To scroll to end of FlatList after displaying the keyboard with React Native, we can set the onLayour prop to a function that calls scrollToEnd on the FlatList's ref. to set onLayout to a function that calls flatListRef. current. scrollToEnd to scroll the FlatList to the end.

Why is FlatList not scrolling?

Simply do following steps to get FlatList scroll. Take out the flex: 1 in your styles. cardcontainer , that should let you scroll. The FlatList/ScrollView contentContainerStyle prop wraps all the child components—what's "inside" the FlatList if you will—and should never have a defined height or flex value.

Is FlatList scrollable by default?

Furthermore, it provides scrolling features by default. You don't need to use extra components like ScrollView to make list data scrollable. The FlatList component also comes with the following features automatically: Header section.

What is scrolltoend bottom in flatlist React Native?

When user clicks on the button then it will automatically scroll to bottom of flatlist using animation even though there are hundreds of items on the list. So in this tutorial we would learn about Example of scrollToEnd Bottom in FlatList React Native. 1.

How to add scroll to bottom functionality on flatlist using animation?

The scrollToEnd function is used to add scroll to bottom functionality on FlatList on button click. We would call the scrollToEnd function from outside using reference of FlatList component. When user clicks on the button then it will automatically scroll to bottom of flatlist using animation even though there are hundreds of items on the list.

Why does the flatlist scroll to the end of the list?

May 31 '18 at 11:31 When the keyboard pops up, the screen has to re-render to adjust its size. This means that the FlatList will also get new dimensions (height) and a layout event will trigger. When this occurs, we scroll to the end of the list: onLayout={() => this.flatList.scrollToEnd({animated: true})}

How often does the scroll position of a flat list reset?

Every time the data source on the flat list changes, say once new data is fetched or prended to the list it resets the scroll position. I’m building some sort of user feed, so I need to be able to update the data source without messing up the users scroll position.


4 Answers

Add style={{flex: 1}} for each View element who is a parent for FlatList.

like image 144
Eugene P. Avatar answered Oct 16 '22 06:10

Eugene P.


I had the same issue and just found the solution. Add the prop ListFooterComponentby giving it a React element/component. I just passed a view with the preferable height and it worked for me perfectly.

Here is the code snippet:

<FlatList
        data={DATA}
        renderItem={({ item }) => (<ListItem itemData={item} />) }
        keyExtractor={item => item.id}
        ListFooterComponent={<View style={{height: 20}}/>}
      />

This works 100%!

like image 41
Vars Zakaryan Avatar answered Oct 16 '22 06:10

Vars Zakaryan


The ultimate solution is ✔

Any view/subview that holds a flatlist must have a flex of 1 - Regardless of how deep.

So if you apply this and it's not working, check your styling and make sure there's no mistake somewhere.

like image 6
Joseph Ajibodu Avatar answered Oct 16 '22 06:10

Joseph Ajibodu


I'm not sure if your scenario is exactly the same as one I encountered on a project a few months ago, but I noticed that I had to add a margin/padding (depends on what you prefer) to lift the bottom of the scrollable container. This was usually because a parent container seemed to interfere with the styling of the scrollable element.

Have you tried adding flexGrow: 1 to your styling in place of flex?

like image 4
Nnanyielugo Avatar answered Oct 16 '22 06:10

Nnanyielugo