facebook / react-native

A framework for building native applications using React
https://reactnative.dev
MIT License
119.15k stars 24.32k forks source link

ReactInstanceManager.recreateReactContextInBackground does not remove the stale root view group #41677

Closed wschurman closed 11 months ago

wschurman commented 11 months ago

New Version

0.73.0-rc.5

Old Version

0.72.5

Build Target(s)

Android in Release Build Variant

Output of react-native info

System:
  OS: macOS 14.1.1
  CPU: (12) arm64 Apple M2 Max
  Memory: 783.33 MB / 64.00 GB
  Shell:
    version: 3.2.57
    path: /bin/bash
Binaries:
  Node:
    version: 18.18.2
    path: /var/folders/6k/nxc22y0n12580hckvvt5dry80000gn/T/yarn--1701206323469-0.5141404886845193/node
  Yarn:
    version: 1.22.19
    path: /var/folders/6k/nxc22y0n12580hckvvt5dry80000gn/T/yarn--1701206323469-0.5141404886845193/yarn
  npm:
    version: 9.8.1
    path: ~/.volta/tools/image/node/18.18.2/bin/npm
  Watchman: Not Found
Managers:
  CocoaPods:
    version: 1.14.3
    path: /opt/homebrew/lib/ruby/gems/2.7.0/bin/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 23.0
      - iOS 17.0
      - macOS 14.0
      - tvOS 17.0
      - watchOS 10.0
  Android SDK:
    API Levels:
      - "26"
      - "28"
      - "29"
      - "30"
      - "31"
      - "33"
      - "34"
    Build Tools:
      - 26.0.3
      - 28.0.3
      - 29.0.2
      - 30.0.2
      - 30.0.3
      - 31.0.0
      - 33.0.0
      - 33.0.0
      - 33.0.1
      - 34.0.0
    System Images:
      - android-26 | Google APIs Intel x86 Atom_64
      - android-28 | Google Play Intel x86 Atom
      - android-30 | Google APIs Intel x86 Atom
      - android-30 | Google Play Intel x86 Atom
      - android-33 | Google Play ARM 64 v8a
    Android NDK: 21.4.7075529
IDEs:
  Android Studio: 2022.3 AI-223.8836.35.2231.10811636
  Xcode:
    version: 15.0.1/15A507
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.9
    path: /opt/homebrew/opt/openjdk@17/bin/javac
  Ruby:
    version: 2.7.8
    path: /opt/homebrew/opt/ruby@2.7/bin/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.2.0
    wanted: 18.2.0
  react-native:
    installed: 0.73.0-rc.5
    wanted: 0.73.0-rc.5
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: false
iOS:
  hermesEnabled: Not found
  newArchEnabled: false

Issue and Reproduction Steps

Issue: calling recreateReactContextInBackground on the ReactInstanceManager instance no longer removes the old root view.

Suspected blame rev: https://github.com/facebook/react-native/pull/37004

Previously, tearDownReactContext would call clearReactRoot, but now it calls detachRootViewFromInstance. In detachRootViewFromInstance, only the view ID is reset: https://github.com/javache/react-native/blob/a63613ca7bffd54211985224a61300d5de0f5640/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java#L1295 as opposed to clearReactRoot which also removes the views from the root view group: https://github.com/javache/react-native/blob/a63613ca7bffd54211985224a61300d5de0f5640/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java#L847

This occurs in both fabric and non-fabric.

Repro: https://github.com/wschurman/rn-reload-repro

The repro repo does the following:

  1. Create app with a custom native module that calls recreateReactContextInBackground.
  2. To see it more clearly, it adds some code to the app that displays the timestamp on mount (so it can be seen that the old view isn't removed when the timestamp doesn't change):
    const [startTime, setStartTime] = React.useState<number | null>(null);
    React.useEffect(() => {
    setStartTime(Date.now());
    }, []);
    ...
    <Text>{startTime}<Text>
lunaleaps commented 11 months ago

Should this have been closed? I'll re-open until someone corrects. Oh sorry I'm confused, this was fixed with https://github.com/facebook/react-native/pull/41678. Closing