react / react/react-native

onViewableItemsChanged isn't correct without specifying the header height in getItemLayout

オープン
#48,351 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Issue: Author Provided Repro Needs: Attention Type: Unsupported Version
主要言語
C++
スター
127k
フォーク
25.3k
平均マージ
1日 23時間
マージ済み PR(30日)
4

説明

Description

I am unsure if this is a bug or expected behaviour. If it's the latter then could be worth adding to the docs.
Also I can only test on my version (0.73.8) unfortunately. But I can't find any issues that suggest it's been fixed recently, so hopefully a useful issue for people. At the very least I couldn't find anything about this online so maybe useful for others.

If I have a simple Flatlist with a header like this:

 <FlatList
            data={Array.from({ length: 20 })}
            renderItem={({ index }) => (
                <V height={index === 1 ? 250 : 150} p={20} dbg>
                    <T>{index}</T>
                </V>
            )}
            getItemLayout={(data, index) => {
                if (index === 0) {
                    return { length: 150, offset: 200, index }
                }
                if (index === 1) {
                    return { length: 250, offset: 150 + 200, index }
                }
                return { length: 150, offset: 150 * (index - 2) + 200 + 150 + 200, index }
            }}
            ListHeaderComponent={<V height={200} width="100%" />}
            viewabilityConfig={viewabilityConfig}
            onViewableItemsChanged={({ viewableItems }) => {
                console.log('>>>', viewableItems, viewableItems.length)
            }}
        />

Screenshot 2024-12-20 at 12 09 48

If I remove the header height (200) from the getItemLayout offset calculation, onViewableItemsChanged fires incorrectly.

If I remove getItemLayout entirely, the logic works fine regardless.

I would have thought by the time the layout has rendered, the onViewableItemsChanged logic would be able to use the actual layout value vs relying on getItemLayout? It would certainly make things robust for us since the header height is quite dependent on various things.

Steps to reproduce

Run the code snippet above. Scroll down and observe that onViewableItemsChanged fires correctly. Remove 200 from getItemLayout calculation and observe it is now incorrect.

React Native Version

0.73.8

Affected Platforms

Runtime - Android, Runtime - iOS

Output of npx react-native info
System:
  OS: macOS 14.4
  CPU: (8) x64 Apple M1 Pro
  Memory: 29.45 MB / 16.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 18.19.1
    path: ~/.nvm/versions/node/v18.19.1/bin/node
  Yarn:
    version: 1.17.3
    path: ~/.yarn/bin/yarn
  npm:
    version: 10.2.4
    path: ~/.nvm/versions/node/v18.19.1/bin/npm
  Watchman:
    version: 2024.12.02.00
    path: /usr/local/bin/watchman
Managers:
  CocoaPods:
    version: 1.15.2
    path: /Users/mattdalton/.rvm/gems/ruby-2.7.7/bin/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 23.5
      - iOS 17.5
      - macOS 14.5
      - tvOS 17.5
      - visionOS 1.2
      - watchOS 10.5
  Android SDK: Not Found
IDEs:
  Android Studio: 2021.2 AI-212.5712.43.2112.8609683
  Xcode:
    version: 15.4/15F31d
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.10
    path: /usr/bin/javac
  Ruby:
    version: 2.7.7
    path: /Users/mattdalton/.rvm/rubies/ruby-2.7.7/bin/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.2.0
    wanted: 18.2.0
  react-native:
    installed: 0.73.8
    wanted: ^0.73.8
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: false
iOS:
  hermesEnabled: true
  newArchEnabled: false
Stacktrace or Logs
N/A
Reproducer

https://github.com/matt-dalton/flatlistviewable

Screenshots and Videos

No response

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

リンクされた flatlistviewable の再現例とその FlatList エントリーポイントから始め、React Native 0.73.8 を使用します。Android と iOS で、getItemLayout の有無、および ListHeaderComponent の 200px オフセットの有無による onViewableItemsChanged の動作を比較します。動作がバグなのか期待されるものなのかを判断し、回帰テストを追加するか、必要なオフセットをドキュメント化すれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript, react-native
領域
mobile
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。