react / react/react-native

Items in a list are unmounted if moved up but not if moved down

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

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

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

説明

Description

https://github.com/user-attachments/assets/ada2ff0b-74d4-43c7-8afc-78830803998d

As visible from the video i have 2 text inputs rendered with a map with stable keys, i focus the one on top and then swap them and they swap keeping the focus on the active one which is what i need. If then i do the same but swapping the focused input from bottom to top it looses focus.

I dived a bit into the RN code and figured out that when going from bottom top top it gets unmounted and then remounted since Differentiator.cpp generated a remove and an insert commands. I understand nothing about how the diffing algo works so i have no idea if this is intendend behaviour or not but it's blocking me with this focus loss problem

I'm reporting the issue on iOS since is where i investigated it the most, on android it works bad as well but in a bit of a different way, with the same code i added in the reproduce section you can see the problem on android as well

Steps to reproduce
import { useState } from "react";
import { View, StyleSheet, TextInput, Button } from "react-native";

export default function TestPage() {
  const [keys, setKeys] = useState<string[]>(["1", "2"]);

  function swap() {
    const newKeys = [...keys];
    const temp = newKeys[0];
    newKeys[0] = newKeys[1];
    newKeys[1] = temp;
    setKeys(newKeys);
  }

  return (
    <View style={styles.container}>
      {keys.map((key, index) => (
        <TextInput
          key={key}
          style={styles.input}
          defaultValue={key == "1" ? "First input" : "Second input"}
        />
      ))}

      <Button title="Swap" onPress={swap} />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
  },
  input: {
    height: 40,
    width: "80%",
    borderColor: "gray",
    borderWidth: 1,
    marginBottom: 10,
    paddingHorizontal: 10,
  },
});
React Native Version

0.79.2

Affected Platforms

Runtime - Android, Runtime - iOS

Output of npx @react-native-community/cli info
System:
  OS: macOS 15.3.2
  CPU: (10) arm64 Apple M1 Pro
  Memory: 864.30 MB / 32.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 20.15.1
    path: ~/.nvm/versions/node/v20.15.1/bin/node
  Yarn:
    version: 1.22.22
    path: ~/.nvm/versions/node/v20.15.1/bin/yarn
  npm:
    version: 10.7.0
    path: ~/.nvm/versions/node/v20.15.1/bin/npm
  Watchman:
    version: 2025.04.28.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.16.2
    path: /Users/ipla/.rvm/gems/ruby-3.3.1/bin/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 24.4
      - iOS 18.4
      - macOS 15.4
      - tvOS 18.4
      - visionOS 2.4
      - watchOS 11.4
  Android SDK:
    Android NDK: 22.1.7171670
IDEs:
  Android Studio: 2024.2 AI-242.23726.103.2422.12816248
  Xcode:
    version: 16.3/16E140
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 21.0.6
    path: /usr/bin/javac
  Ruby:
    version: 3.3.1
    path: /Users/ipla/.rvm/rubies/ruby-3.3.1/bin/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 19.0.0
    wanted: 19.0.0
  react-native:
    installed: 0.79.2
    wanted: 0.79.2
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: Not found
  newArchEnabled: Not found
iOS:
  hermesEnabled: Not found
  newArchEnabled: Not found
Stacktrace or Logs
Doesn't apply
MANDATORY Reproducer

https://snack.expo.dev/@ipla/rn-swap-reproducer

Screenshots and Videos

https://github.com/user-attachments/assets/3ea384d0-5762-4fb3-bec1-7d677c316b3f

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

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

はじめの一歩

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

調査の方向性

必須のSnack reproducerから始め、Differentiator.cpp周辺のReact Nativeのdiffingパスを調査し、フォーカスされたinputがそれぞれの方向に移動したときに生成されるコマンドを比較します。reproducerで示されているように、iOSとAndroidの両方で、2つのkeyed TextInput要素が両方向の入れ替え後もフォーカスを保持すれば完了です。

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

評価

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

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

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