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.

Infinite scroll with items with different size

See original GitHub issue

Current behavior

When onEndReached callback execute and new items are being added to the list, we see content jumping (list is shifting to top or bottom automatically)

Expected behavior

When onEndReached callback execute and new items are being added to the list, list must stay at the same place where it was before new items were added

To Reproduce

Let say average item height is 100 and floating in size is between 100-200. Also I’ve sighted that shifting is more agressive when renderItem contains complecated components, for example 10 Views, 1 ImageView and 3 TextViews, and also when scrolling is fast

https://user-images.githubusercontent.com/60893275/195725090-885ceb55-b6cf-4d4c-a07d-1bf8db1b3d23.mp4

import React, { useState } from 'react';
import { SafeAreaView, StyleSheet, Text, View } from 'react-native';
import { FlashList } from '@shopify/flash-list';
import { chunk, range } from 'lodash';

const ITEMS = range(1000);
const CHUNKS = chunk(ITEMS, 20);

export const Screen = () => {
  const [page, setPage] = useState(1);
  const [data, setData] = useState(CHUNKS[0]);

  const renderItem = ({ item }) => {
    return (
      <View style={{ height: 100 + Math.random() * 100 }}>
        <View style={styles.item}>
          <Text>Item index {item}</Text>
        </View>
      </View>
    );
  };

  return (
    <SafeAreaView style={styles.flex}>
      <FlashList
        data={data}
        estimatedItemSize={100}
        renderItem={renderItem}
        onEndReached={() => {
          setData((prev) => [...prev, ...CHUNKS[page]]);
          setPage((prev) => prev + 1);
        }}
      />
    </SafeAreaView>
  );
};

const styles = StyleSheet.create({
  flex: { flex: 1 },
  item: {
    flex: 1,
    backgroundColor: 'aqua',
    margin: 20,
    borderRadius: 24,
    justifyContent: 'center',
    alignItems: 'center',
  },
});

Platform:

  • iOS
  • Android

Environment

  "react": "18.0.0"
  "react-native": "0.69.1"
  "@shopify/flash-list": "^1.3.0"

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

1reaction
Navipro70commented, Oct 14, 2022

Sorry, I’ve not seen that Math.random() is recalculate height for every rerender, but I had this bug with real server and layout (without random), and after some hours of researching, I saw that my requests and data storing are not correct, with valid data it works fine. I close the issue, thanks for the lib!

0reactions
Navipro70commented, Dec 7, 2022

@joesermon Hello, I think trouble has been RTK-Query in my case, maybe it’s convert data wrongly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Loading tables of different dimensions in infinite scroll page ...
I have a website where users contribute with content. The content takes form of tables, where every td element is of equal height...
Read more >
Infinite Scroll'ing the right way | Walmart Global Tech Blog |
Scrolling is an implementation of data display whenever we cannot fit everything onto a single page. Most websites are generally implemented ...
Read more >
Perfect Infinite Scroll UX - Smashing Magazine
The easiest way to improve infinite scroll is by marking the breaks between “new” and “old” items in the list. When a new...
Read more >
Infinite scroll with Masonry and reloadItems (different sizes)
I have a view with infinite scroll and a responsive Masonry grid ( hidden link ) where the items are of different sizes....
Read more >
Extras - Infinite Scroll
Everything else about Infinite Scroll: module loaders, loading JSON, extra demos.
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