react / react/react-native

SectionList.scrollToLocation not scrolling to section with itemIndex: 0

Open
#50,143 3 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Component: SectionList Issue: Author Provided Repro
Dominant language
C++
Stars
127k
Forks
25.3k
Avg merge
1d 23h
Merged PRs (30d)
4

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the SectionList.scrollToLocation implementation and the linked Snack reproducer, comparing sectionIndex: 1 with itemIndex: 0 versus 1 on iOS under React Native 0.76.7. Trace how the target section is resolved for itemIndex: 0 and verify the fix against the reported case, with the second section scrolling into view without regressions.

Written by the indexing model from the issue text.

Assessment

Tech stack
ios, javascript, react-native
Domain
mobile
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.