question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

maintainVisibleContentPosition does not work correctly with multiple instances of a Flatlist

See original GitHub issue

I’m currently facing an issue where the <Flatlist/> component is failing to maintain its position when being used as a common component between multiple other components. My use case goes the following way. I have a common component <Feed/> that holds common logic and renders this package <Flatlist/>. This <Feed/> component is being used from 2 other components, so there are <Home/> and <Verticals/>, both have an instance of <Feed/> in their render functions. The <Home/> is always mounted, meaning that it is only being unmounted when the app is killed, but the <Verticals/> one is unmounted when the user navigates back from that specific screen and mounted when tapping that screen. The Flatlist works as expected and maintains its position only at the <Home/> screen. When I navigate to the <Verticals/> and update the content for that list, the view is not being maintained.

I noticed that if I do not render the <Feed/> component inside the <Home/> screen and only in the <Verticals/> screen, the list behaves as expected and the view is maintained. I tried to do some debugging but I couldn’t find anything useful for me to find a solution (so far). I did inspect flRef.current.getScrollableNode() for the <Verticals/> list and I saw that the values were different for those scenarios. Meaning that when I first loaded the Home list screen and then navigate to the Verticals screen that value was different if I only render the Vertical list screen (when is actually working).

Edit: One more thing, If I go to the main list in the Home screen (currently working), go to the other instance of the list in the Vertical screen (not working) and then go back to the Home screen, now the list in the Home screen does not mantains its position.

This is the configuration for the list in the common <Feed/>

    <FlatList
        ref={ref}
        style={extraStyles}
        accessibilityLabel="Content Feed"
        ListHeaderComponent={header}
        ListEmptyComponent={LoadingStuff}
        data={feedItems}
        renderItem={renderItem}
        onTouchStart={onTouchStart}
        onViewableItemsChanged={onViewableItemsChanged}
        viewabilityConfig={viewabilityConfig}
        keyboardShouldPersistTaps="handled"
        keyExtractor={keyExtractor}
        onEndReached={onInfiniteScroll}
        onEndReachedThreshold={0}
        showsVerticalScrollIndicator={false}
        onScroll={onScroll}
        scrolled={scrolled}
        maintainVisibleContentPosition={{
          minIndexForVisible: 0,
        }}
        refreshControl={
          <RefreshControl refreshing={isRefreshing} onRefresh={onRefresh} />
        }
      />

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10

github_iconTop GitHub Comments

3reactions
jbernhardt23commented, Jun 10, 2021
2reactions
jbernhardt23commented, Jun 3, 2021

hey @bradnorman5490 ! yes, we have finished doing extensive testing around this on our side so I will be opening a PR shortly (tomorrow or starting next week)

Read more comments on GitHub >

github_iconTop Results From Across the Web

FlatList maintainVisibleContentPosition sometimes doesn't ...
this basically works fine, but in below condition, it doesn't work. condition: current items length is equal or smaller than added items.
Read more >
FlatList - React Native
This is a PureComponent which means that it will not re-render if props remain shallow-equal. Make sure that everything your renderItem function ......
Read more >
Displaying a List in React Native: Map Method or FlatList ...
In this article, I'll walk you through using two methods of listing data in a React Native app: displaying the list with map...
Read more >
React Native: How To Build Bidirectional Infinite Scroll
FlatList from React Native has built-in support for infinite scroll ... scroll doesn't jump to the end of the list when more items...
Read more >
[Android] maintainVisibleContentPosition on FlatList - Expo
This package fixes the issue but is not available for managed Expo projects: ... I've tested flat-list-mvcp with EAS Build and it seems...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found