react / react/react-native

FlatList with useState and stickyHeaderIndices is crashing on Android but working fine on IOS

Offen
#53,258 3 Kommentare 9 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Component: FlatList Issue: Author Provided Repro Needs: Author Feedback Needs: React Native Team Attention Platform: Android Platform: iOS
Vorherrschende Sprache
C++
Sterne
127k
Forks
25.3k
Ø Merge
1 T. 23 Std.
Gemergte PRs (30 T.)
4

Beschreibung

Description

On Android, FlatList crashes if:

  • data is stored in a state variable and updated in useEffect.
  • stickyHeaderIndices is set.

If the same data is passed as a constant (instead of state), the crash does not occur.

This only happens on Android — iOS works fine.

A similar issue was reported before ([#25157](https://github.com/facebook/react-native/issues/25157)) but closed, yet the bug still exists in the latest version.

Steps to Reproduce:

  1. Create a FlatList with stickyHeaderIndices set.
  2. Set data to a state variable, initially empty.
  3. Use useEffect to update the state variable with an array after mount.
  4. Run the app on Android.
  5. The app crashes.

Expected Behavior:
The FlatList should render with sticky headers without crashing when the data is updated via state in useEffect.

Actual Behavior:
The app crashes on Android when data is updated from state inside useEffect if stickyHeaderIndices is used.

Example Code:

import React, { useState, useEffect } from 'react';
import { FlatList, Text, View } from 'react-native';

export default function App() {
  const [listData, setListData] = useState([]);

  useEffect(() => {
    setListData([
      { key: '1', title: 'Header' },
      { key: '2', title: 'Item 1' },
      { key: '3', title: 'Item 2' }
    ]);
  }, []);

  return (
    <View>
      <FlatList
        data={listData}
        stickyHeaderIndices={[0]} // causes crash on Android
        renderItem={({ item }) => <Text>{item.title}</Text>}
      />
    </View>
  );
}

// Works fine if using:
// const listData = [
//   { key: '1', title: 'Header' },
//   { key: '2', title: 'Item 1' },
//   { key: '3', title: 'Item 2' }
// ];
Steps to reproduce

import React, { useState, useEffect } from 'react';
import { FlatList, Text, View } from 'react-native';

export default function App() {
const [listData, setListData] = useState([]);

useEffect(() => {
setListData([
{ key: '1', title: 'Header' },
{ key: '2', title: 'Item 1' },
{ key: '3', title: 'Item 2' }
]);
}, []);

return (

<FlatList
data={listData}
stickyHeaderIndices={[0]} // causes crash on Android
renderItem={({ item }) => {item.title}}
/>

);
}

// Works fine if using:
// const listData = [
// { key: '1', title: 'Header' },
// { key: '2', title: 'Item 1' },
// { key: '3', title: 'Item 2' }
// ];

React Native Version

v0.81.0

Affected Platforms

Runtime - Android

Output of npx @react-native-community/cli info
info Fetching system and libraries information...
System:
  OS: macOS 15.4
  CPU: (12) x64 Intel(R) Core(TM) i5-10600 CPU @ 3.30GHz
  Memory: 129.52 MB / 32.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 21.1.0
    path: /usr/local/bin/node
  Yarn:
    version: 1.22.19
    path: /usr/local/bin/yarn
  npm:
    version: 10.2.0
    path: /usr/local/bin/npm
  Watchman: Not Found
Managers:
  CocoaPods:
    version: 1.14.3
    path: /usr/local/bin/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 24.2
      - iOS 18.2
      - macOS 15.2
      - tvOS 18.2
      - visionOS 2.2
      - watchOS 11.2
  Android SDK: Not Found
IDEs:
  Android Studio: 2020.3 AI-203.7717.56.2031.7621141
  Xcode:
    version: 16.2/16C5032a
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.15
    path: /Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home/bin/javac
  Ruby:
    version: 3.3.3
    path: /Users/hassan/.rbenv/shims/ruby
npmPackages:
  "@react-native-community/cli":
    installed: 19.0.0
    wanted: 19.0.0
  react:
    installed: 19.1.0
    wanted: 19.1.0
  react-native:
    installed: 0.80.1
    wanted: 0.80.1
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: true
iOS:
  hermesEnabled: true
  newArchEnabled: true

info React Native v0.81.0 is now available (your project is running on v0.80.1).
info Changelog: https://github.com/facebook/react-native/releases/tag/v0.81.0
info Diff: https://react-native-community.github.io/upgrade-helper/?from=0.80.1&to=0.81.0
info For more info, check out "https://reactnative.dev/docs/upgrading?os=macos".
Stacktrace or Logs
addViewAt: failed to insert view [664] into parent [646] at index 0
MANDATORY Reproducer

https://snack.expo.dev/@hassanalvi11/crash-app

Screenshots and Videos
Image

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Führe den obligatorischen Snack-Reproduzierer aus und verifiziere den nur auf Android auftretenden Absturz, wenn zustandsbasierte Daten in useEffect mit stickyHeaderIndices befüllt werden. Verfolge den FlatList-Aktualisierungspfad rund um den gemeldeten Einfügungsfehler von addViewAt; abgeschlossen ist die Aufgabe, wenn dasselbe Beispiel auf Android gerendert wird und das Sticky-Header-Verhalten ohne Absturz beibehält.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
android, javascript, react-native
Bereich
mobile
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
50/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.