Guard against unmounted components when accessing public instances on Fabric (#27687) (#41452)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/41452

## Summary

This fixes an error in `getPublicInstanceFromInstanceHandle` where we
throw an error when trying to access the public instance from the fiber
of an unmounted component. This shouldn't throw but return `null`
instead.

## How did you test this change?

Updated unit tests.
Before:
<img width="969" alt="Screenshot 2023-11-10 at 15 26 14"
src="https://github.com/facebook/react/assets/117921/ea161616-2775-4fab-8d74-da4bef48d09a">

After:
<img width="1148" alt="Screenshot 2023-11-10 at 15 28 37"
src="https://github.com/facebook/react/assets/117921/db18b918-b6b6-4925-9cfc-3b4b2f3ab92d">

DiffTrain build for commit https://github.com/facebook/react/commit/6b3834a45b585e4340734139841ae81dc1b1a75d.

Test Plan: Sandcastle tests

Reviewed By: tyao1

Differential Revision: D51203276

Pulled By: rubennorte

fbshipit-source-id: 40c471843dea91573bff26f8595eb9554923e5d8
This commit is contained in:
rubennorte (Meta Employee)
2023-11-14 06:11:13 -08:00
committed by Facebook GitHub Bot
parent 686f83e6fe
commit e0aa9abfaf
@@ -7,7 +7,7 @@
* @noformat
* @flow strict
* @nolint
* @generated SignedSource<<652b117c94307244bcf5e4af18928903>>
* @generated SignedSource<<1836a1b6639552dce12199ef2c85f63d>>
*/
import type {ElementRef, ElementType, Element, AbstractComponent} from 'react';
@@ -247,7 +247,7 @@ export type ReactFabricType = {
): ?Node,
getPublicInstanceFromInternalInstanceHandle(
internalInstanceHandle: InternalInstanceHandle,
): PublicInstance | PublicTextInstance,
): PublicInstance | PublicTextInstance | null,
...
};