react / react/react-native

SectionList.scrollToLocation not scrolling to section with itemIndex: 0

未关闭
#50,143 3 条评论 4 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Component: SectionList Issue: Author Provided Repro
主要语言
C++
星标
127k
派生
25.3k
平均合并
1 天 23 小时
30 天内合并 PR
4

描述

Description

When using the SectionList.scrollToLocation method with itemIndex: 0, the SectionList does not scroll to the expected section. The scroll works when itemIndex is set to 1. No errors are logged, and the onScrollToIndexFailed callback is never triggered.

Steps to reproduce

https://snack.expo.dev/@markwitt/sectionlist

1.	Create a SectionList with multiple sections and items with varying heights (see the code example below).
2.	Add a button that calls scrollToLocation with parameters sectionIndex: 1 and itemIndex: 0 to scroll to the second section (e.g., “Vegetables”).
3.	Press the button and observe that the list does not scroll to the desired section.
4.	Change itemIndex to 1 and notice that the scrolling occurs as expected.
import React, { useRef, useMemo } from 'react';
import { SectionList, Text, View, Button } from 'react-native';

// Function to generate sections with a specified number of items per section
const generateSections = () => {
 const createItems = (prefix, count) => {
   return Array.from({ length: count }, (_, i) => ({
     label: `${prefix} ${i + 1}`,
     // Random height between 50 and 150
     height: 50 + Math.floor(Math.random() * 100),
   }));
 };

 return [
   { title: 'Fruits', data: createItems('Fruit', 15) },
   { title: 'Vegetables', data: createItems('Vegetable', 15) },
   { title: 'Grains', data: createItems('Grain', 15) },
 ];
};

export default function App() {
 const sectionListRef = useRef(null);
 // useMemo to generate sections only once on mount
 const sections = useMemo(() => generateSections(), []);

 const scrollToSection = () => {
   if (sectionListRef.current) {
     sectionListRef.current.scrollToLocation({
       sectionIndex: 1, // "Vegetables" section
       itemIndex: 0,    // Using 0 does not scroll; using 1 works correctly
     });
   }
 };

 return (
   <View style={{ flex: 1, paddingTop: 40 }}>
     <Button title="Scroll to Vegetables" onPress={scrollToSection} />
     <SectionList
       ref={sectionListRef}
       sections={sections}
       keyExtractor={(item, index) => item.label + index}
       renderItem={({ item }) => (
         <View
           style={{
             height: item.height,
             justifyContent: 'center',
             borderBottomWidth: 1,
             borderColor: '#ccc',
           }}
         >
           <Text style={{ padding: 10 }}>
             {item.label} (Height: {item.height})
           </Text>
         </View>
       )}
       renderSectionHeader={({ section: { title } }) => (
         <Text
           style={{
             padding: 10,
             fontWeight: 'bold',
             backgroundColor: '#eee',
           }}
         >
           {title}
         </Text>
       )}
     />
   </View>
 );
}````


### React Native Version

0.76.7

### Affected Platforms

Runtime - iOS

### Output of `npx @react-native-community/cli info`

```text
npx @react-native-community/cli info 
(node:84710) ExperimentalWarning: CommonJS module /Users/mark/.nvm/versions/node/v23.2.0/lib/node_modules/npm/node_modules/debug/src/node.js is loading ES Module /Users/mark/.nvm/versions/node/v23.2.0/lib/node_modules/npm/node_modules/supports-color/index.js using require().
Support for loading ES Module in require() is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
Need to install the following packages:
@react-native-community/cli@18.0.0
Ok to proceed? (y) y

(node:84739) ExperimentalWarning: CommonJS module /Users/mark/Projects/hivemind-expo/node_modules/debug/src/node.js is loading ES Module /Users/mark/Projects/hivemind-expo/node_modules/debug/node_modules/supports-color/index.js using require().
Support for loading ES Module in require() is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
(node:84739) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
info Fetching system and libraries information...
System:
 OS: macOS 15.2
 CPU: (10) arm64 Apple M1 Pro
 Memory: 189.44 MB / 16.00 GB
 Shell:
   version: "5.9"
   path: /bin/zsh
Binaries:
 Node:
   version: 23.2.0
   path: ~/.nvm/versions/node/v23.2.0/bin/node
 Yarn:
   version: 1.22.22
   path: ~/.nvm/versions/node/v23.2.0/bin/yarn
 npm:
   version: 10.9.0
   path: ~/.nvm/versions/node/v23.2.0/bin/npm
 Watchman: Not Found
Managers:
 CocoaPods:
   version: 1.16.2
   path: /opt/homebrew/bin/pod
SDKs:
 iOS SDK:
   Platforms:
     - DriverKit 24.1
     - iOS 18.1
     - macOS 15.1
     - tvOS 18.1
     - visionOS 2.1
     - watchOS 11.1
 Android SDK: Not Found
IDEs:
 Android Studio: 2024.2 AI-242.23339.11.2421.12550806
 Xcode:
   version: 16.1/16B40
   path: /usr/bin/xcodebuild
Languages:
 Java:
   version: 23.0.2
   path: /opt/homebrew/opt/openjdk/bin/javac
 Ruby:
   version: 2.6.10
   path: /usr/bin/ruby
npmPackages:
 "@react-native-community/cli": Not Found
 react:
   installed: 18.3.1
   wanted: 18.3.1
 react-native:
   installed: 0.76.7
   wanted: 0.76.7
 react-native-macos: Not Found
npmGlobalPackages:
 "*react-native*": Not Found
Android:
 hermesEnabled: Not found
 newArchEnabled: Not found
iOS:
 hermesEnabled: Not found
 newArchEnabled: Not found

info React Native v0.78.1 is now available (your project is running on v0.76.7).
info Changelog: https://github.com/facebook/react-native/releases/tag/v0.78.1
info Diff: https://react-native-community.github.io/upgrade-helper/?from=0.76.7&to=0.78.1
info For more info, check out "https://reactnative.dev/docs/upgrading?os=macos".
Stacktrace or Logs
...
Reproducer

https://snack.expo.dev/@markwitt/sectionlist

Screenshots and Videos

No response

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 SectionList.scrollToLocation 的实现和链接的 Snack 复现代码开始,在 iOS 的 React Native 0.76.7 上比较 sectionIndex: 1 配合 itemIndex: 0 与配合 1 的情况。跟踪 itemIndex: 0 时目标 section 的解析方式,并针对报告的案例验证修复,确保第二个 section 在没有回归的情况下滚动到可见区域。

由索引模型根据 Issue 内容生成。

评估

技术栈
ios, javascript, react-native
领域
mobile
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
活跃
描述清晰度
基本清楚
新手友好度
55/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。