Skip to content

Commit 02ea417

Browse files
rubennortefacebook-github-bot
authored andcommitted
Prepare for next React Native sync with new instance format (facebook#36438)
Summary: Pull Request resolved: facebook#36438 This makes the `react-native` repository compatible with the next sync from react after the changes in facebook/react#26321 land. That PR is changing the format of the Fabric instance and we have a few instances where we assume the internal structure of that instance in the repository. Changelog: [internal] Differential Revision: D43980374 fbshipit-source-id: d2e5c03dc595bd181fbdfb1bed28155dd3840e40
1 parent 801b005 commit 02ea417

File tree

5 files changed

+51
-16
lines changed

5 files changed

+51
-16
lines changed

Libraries/Animated/useAnimatedProps.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
'use strict';
1212

13+
import {isPublicInstance as isFabricPublicInstance} from '../Renderer/public/ReactFabricPublicInstance';
1314
import useRefEffect from '../Utilities/useRefEffect';
1415
import {AnimatedEvent} from './AnimatedEvent';
1516
import NativeAnimatedHelper from './NativeAnimatedHelper';
@@ -183,7 +184,7 @@ function getEventTarget<TInstance>(instance: TInstance): TInstance {
183184
// $FlowFixMe[unclear-type] - Legacy instance assumptions.
184185
function isFabricInstance(instance: any): boolean {
185186
return (
186-
hasFabricHandle(instance) ||
187+
isFabricPublicInstance(instance) ||
187188
// Some components have a setNativeProps function but aren't a host component
188189
// such as lists like FlatList and SectionList. These should also use
189190
// forceUpdate in Fabric since setNativeProps doesn't exist on the underlying
@@ -192,13 +193,9 @@ function isFabricInstance(instance: any): boolean {
192193
// If these components end up using forwardRef then these hacks can go away
193194
// as instance would actually be the underlying host component and the above check
194195
// would be sufficient.
195-
hasFabricHandle(instance?.getNativeScrollRef?.()) ||
196-
hasFabricHandle(instance?.getScrollResponder?.()?.getNativeScrollRef?.())
196+
isFabricPublicInstance(instance?.getNativeScrollRef?.()) ||
197+
isFabricPublicInstance(
198+
instance?.getScrollResponder?.()?.getNativeScrollRef?.(),
199+
)
197200
);
198201
}
199-
200-
// $FlowFixMe[unclear-type] - Legacy instance assumptions.
201-
function hasFabricHandle(instance: any): boolean {
202-
// eslint-disable-next-line dot-notation
203-
return instance?.['_internalInstanceHandle']?.stateNode?.canonical != null;
204-
}

Libraries/Components/ScrollView/ScrollViewStickyHeader.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import type {LayoutEvent} from '../../Types/CoreEventTypes';
1212

1313
import Animated from '../../Animated/Animated';
14+
import {isPublicInstance as isFabricPublicInstance} from '../../Renderer/public/ReactFabricPublicInstance';
1415
import StyleSheet from '../../StyleSheet/StyleSheet';
1516
import Platform from '../../Utilities/Platform';
1617
import useMergeRefs from '../../Utilities/useMergeRefs';
@@ -64,10 +65,7 @@ const ScrollViewStickyHeaderWithForwardedRef: React.AbstractComponent<
6465
ref.setNextHeaderY = value => {
6566
setNextHeaderLayoutY(value);
6667
};
67-
// Avoid dot notation because at Meta, private properties are obfuscated.
68-
// $FlowFixMe[prop-missing]
69-
const _internalInstanceHandler = ref['_internalInstanceHandle']; // eslint-disable-line dot-notation
70-
setIsFabric(Boolean(_internalInstanceHandler?.stateNode?.canonical));
68+
setIsFabric(isFabricPublicInstance(ref));
7169
};
7270
const ref: (React.ElementRef<typeof Animated.View> | null) => void =
7371
// $FlowFixMe[incompatible-type] - Ref is mutated by `callbackRef`.

Libraries/Components/TraceUpdateOverlay/TraceUpdateOverlay.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ interface Agent {
3434
}
3535

3636
type TraceNode = {
37+
publicInstance?: TraceNode,
38+
// TODO: remove this field when syncing the new version of the renderer from React to React Native.
3739
canonical?: TraceNode,
3840
measure?: (
3941
(
@@ -100,7 +102,11 @@ export default function TraceUpdateOverlay(): React.Node {
100102

101103
const newFramesToDraw: Array<Promise<Overlay>> = [];
102104
nodesToDraw.forEach(({node, color}) => {
103-
const component = node.canonical ?? node;
105+
// `publicInstance` => Fabric
106+
// TODO: remove this check when syncing the new version of the renderer from React to React Native.
107+
// `canonical` => Legacy Fabric
108+
// `node` => Legacy renderer
109+
const component = node.publicInstance ?? node.canonical ?? node;
104110
if (!component || !component.measure) {
105111
return;
106112
}

Libraries/Inspector/DevtoolsOverlay.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,12 @@ export default function DevtoolsOverlay({
5151

5252
function onAgentShowNativeHighlight(node: any) {
5353
clearTimeout(hideTimeoutId);
54-
// Shape of `node` is different in Fabric.
55-
const component = node.canonical ?? node;
54+
55+
// `publicInstance` => Fabric
56+
// TODO: remove this check when syncing the new version of the renderer from React to React Native.
57+
// `canonical` => Legacy Fabric
58+
// `node` => Legacy renderer
59+
const component = node.publicInstance ?? node.canonical ?? node;
5660
if (!component || !component.measure) {
5761
return;
5862
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
* @flow strict
9+
*/
10+
11+
export function isPublicInstance(maybeInstance: mixed): boolean {
12+
return (
13+
maybeInstance != null &&
14+
// TODO: implement a better check (maybe using instanceof) when the instance is defined in the React Native repository.
15+
(maybeInstance.__nativeTag != null ||
16+
// TODO: remove this check when syncing the new version of the renderer from React to React Native.
17+
isLegacyFabricInstance(maybeInstance))
18+
);
19+
}
20+
21+
function isLegacyFabricInstance(maybeInstance: mixed): boolean {
22+
/* eslint-disable dot-notation */
23+
return (
24+
maybeInstance != null &&
25+
// $FlowExpectedError[incompatible-use]
26+
maybeInstance['_internalInstanceHandle'] != null &&
27+
maybeInstance['_internalInstanceHandle'].stateNode != null &&
28+
maybeInstance['_internalInstanceHandle'].stateNode.canonical != null
29+
);
30+
}

0 commit comments

Comments
 (0)