react / react/react-native

maintainVisibleContentPosition incorrectly autoscrolls on ios

オープン
#42,915 コメント 7 件 リアクション 2 件 担当者 0 名 GitHub で見る

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

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

説明

Description

What I want is for flatlist to maintain its position if the offset is larger than 10, otherwise automatically scroll to new content, so I am using the maintainVisibleContentPosition with values of { minIndexForVisible: 0, autoscrollToTopThreshold: 10, }. Now this works correctly on android but not on ios.

Here is a small reproducible or its snack:

import React, {useState} from 'react';
import {
  SafeAreaView,
  View,
  FlatList,
  StyleSheet,
  Text,
  StatusBar,
  Button,
} from 'react-native';

type ItemProps = {title: string};

const Item = ({title}: ItemProps) => (
  <View style={styles.item}>
    <Text style={styles.title}>{title}</Text>
  </View>
);

const App = () => {
  const [data, setData] = useState([1, 2, 3, 4, 5, 6, 7, 8, 9]);

  const addToTop = () => {
    setData([data[0] - 1, ...data]);
  };

  const removeFromTop = () => {
    const [_, ...rest] = data;
    setData(rest);
  };

  return (
    <SafeAreaView style={styles.container}>
      <FlatList
        maintainVisibleContentPosition={{ minIndexForVisible: 0, autoscrollToTopThreshold: 10, }}
        data={data}
        renderItem={({item}) => <Item title={`Item ${item}`} />}
        keyExtractor={item => item.toString()}
      />
      <Button title="add to top" onPress={addToTop} />
      <Button title="remove from top" onPress={removeFromTop} />
    </SafeAreaView>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    marginTop: StatusBar.currentHeight || 0,
  },
  item: {
    backgroundColor: '#f9c2ff',
    padding: 20,
    marginVertical: 8,
    marginHorizontal: 16,
    height: 480,
  },
  title: {
    fontSize: 32,
  },
});

export default App;
Steps to reproduce
  1. Copy the repro above to some rn project
  2. Scroll to half of the first element, then press addToTop, on android position will be maintained which is correct but on ios it autoscrolls.
React Native Version

0.73.4

Affected Platforms

Runtime - iOS

Output of npx react-native info
System:
  OS: macOS 14.1.2
  CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
  Memory: 2.57 GB / 16.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 18.17.1
    path: ~/.nvm/versions/node/v18.17.1/bin/node
  Yarn:
    version: 1.22.19
    path: ~/.nvm/versions/node/v18.17.1/bin/yarn
  npm:
    version: 9.6.7
    path: ~/.nvm/versions/node/v18.17.1/bin/npm
  Watchman:
    version: 2024.01.22.00
    path: /usr/local/bin/watchman
Managers:
  CocoaPods:
    version: 1.15.0
    path: /Users/ramiel/.rbenv/shims/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 23.2
      - iOS 17.2
      - macOS 14.2
      - tvOS 17.2
      - visionOS 1.0
      - watchOS 10.2
  Android SDK: Not Found
IDEs:
  Android Studio: 2023.1 AI-231.9392.1.2311.11076708
  Xcode:
    version: 15.2/15C500b
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 11.0.15
    path: /usr/local/opt/openjdk@11/bin/javac
  Ruby:
    version: 2.7.6
    path: /Users/ramiel/.rbenv/shims/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.2.0
    wanted: 18.2.0
  react-native:
    installed: 0.73.4
    wanted: 0.73.4
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: false
iOS:
  hermesEnabled: true
  newArchEnabled: false


### Stacktrace or Logs

```text
No Stacktrace or Logs
Reproducer

https://snack.expo.dev/@ramiel99/spunky-green-peanut

Screenshots and Videos

https://github.com/facebook/react-native/assets/80689446/456e98f1-d5a8-4253-9a53-9b8d08e61426

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

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

はじめの一歩

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

調査の方向性

まず、リンクされた Snack を iOS と Android で実行し、add-to-top と remove-from-top アクションで FlatList の maintainVisibleContentPosition を試します。報告されたしきい値付近での FlatList の動作を追跡し、その後、iOS が 10 を超えている場合は offset を維持し、しきい値以内の場合にのみ自動スクロールすることを、Android と一致する形で確認します。

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

評価

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

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

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