Skip to content

Commit 9c54b29

Browse files
authored
Remove ReactFabricPublicInstance and used definition from ReactNativePrivateInterface (#26437)
## Summary Now that React Native owns the definition for public instances in Fabric and ReactNativePrivateInterface provides the methods to create instances and access private fields (see facebook/react-native#36570), we can remove the definitions from React. After this PR, React Native public instances will be opaque types for React and it will only handle their creation but not their definition. This will make RN similar to DOM in how public instances are handled. This is a new version of #26418 which was closed without merging. ## How did you test this change? * Existing tests. * Manually synced the changes in this PR to React Native and tested it end to end in Meta's infra.
1 parent f77099b commit 9c54b29

13 files changed

+96
-455
lines changed

Diff for: packages/react-native-renderer/src/ReactFabricHostConfig.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
*/
99

1010
import type {TouchedViewDataAtPoint, ViewConfig} from './ReactNativeTypes';
11-
import {
12-
createPublicInstance,
13-
type ReactFabricHostComponent,
14-
} from './ReactFabricPublicInstance';
1511
import {create, diff} from './ReactNativeAttributePayload';
1612
import {dispatchEvent} from './ReactFabricEventEmitter';
1713
import {
@@ -23,6 +19,8 @@ import {
2319
import {
2420
ReactNativeViewConfigRegistry,
2521
deepFreezeAndThrowOnMutationInDev,
22+
createPublicInstance,
23+
type PublicInstance as ReactNativePublicInstance,
2624
} from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface';
2725

2826
const {
@@ -62,12 +60,12 @@ export type Instance = {
6260
// Reference to the React handle (the fiber)
6361
internalInstanceHandle: Object,
6462
// Exposed through refs.
65-
publicInstance: ReactFabricHostComponent,
63+
publicInstance: PublicInstance,
6664
},
6765
};
6866
export type TextInstance = {node: Node, ...};
6967
export type HydratableInstance = Instance | TextInstance;
70-
export type PublicInstance = ReactFabricHostComponent;
68+
export type PublicInstance = ReactNativePublicInstance;
7169
export type Container = number;
7270
export type ChildSet = Object;
7371
export type HostContext = $ReadOnly<{

Diff for: packages/react-native-renderer/src/ReactFabricPublicInstance.js

-153
This file was deleted.

Diff for: packages/react-native-renderer/src/ReactFabricPublicInstanceUtils.js

-36
This file was deleted.

Diff for: packages/react-native-renderer/src/ReactNativeFiberInspector.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ import {
1717
import getComponentNameFromType from 'shared/getComponentNameFromType';
1818
import {HostComponent} from 'react-reconciler/src/ReactWorkTags';
1919
// Module provided by RN:
20-
import {UIManager} from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface';
20+
import {
21+
UIManager,
22+
getNodeFromPublicInstance,
23+
} from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface';
2124
import {enableGetInspectorDataForInstanceInProduction} from 'shared/ReactFeatureFlags';
2225
import {getClosestInstanceFromNode} from './ReactNativeComponentTree';
23-
import {getNodeFromPublicInstance} from './ReactFabricPublicInstanceUtils';
2426
import {getNodeFromInternalInstanceHandle} from './ReactNativePublicCompat';
2527

2628
const emptyObject = {};

Diff for: packages/react-native-renderer/src/ReactNativePublicCompat.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import type {ElementRef, ElementType} from 'react';
1414
import {
1515
UIManager,
1616
legacySendAccessibilityEvent,
17+
getNodeFromPublicInstance,
18+
getNativeTagFromPublicInstance,
1719
} from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface';
1820

1921
import {
@@ -23,11 +25,6 @@ import {
2325
import ReactSharedInternals from 'shared/ReactSharedInternals';
2426
import getComponentNameFromType from 'shared/getComponentNameFromType';
2527

26-
import {
27-
getNodeFromPublicInstance,
28-
getNativeTagFromPublicInstance,
29-
} from './ReactFabricPublicInstanceUtils';
30-
3128
const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;
3229

3330
export function findHostInstance_DEPRECATED<TElementType: ElementType>(
@@ -83,6 +80,7 @@ export function findHostInstance_DEPRECATED<TElementType: ElementType>(
8380

8481
// findHostInstance handles legacy vs. Fabric differences correctly
8582
// $FlowFixMe[incompatible-exact] we need to fix the definition of `HostComponent` to use NativeMethods as an interface, not as a type.
83+
// $FlowFixMe[incompatible-return]
8684
return hostInstance;
8785
}
8886

@@ -147,9 +145,8 @@ export function findNodeHandle(componentOrHandle: any): ?number {
147145
return hostInstance;
148146
}
149147

150-
// $FlowFixMe[prop-missing] For compatibility with legacy renderer instances
148+
// $FlowFixMe[incompatible-type] For compatibility with legacy renderer instances
151149
if (hostInstance._nativeTag != null) {
152-
// $FlowFixMe[incompatible-return]
153150
return hostInstance._nativeTag;
154151
}
155152

Diff for: packages/react-native-renderer/src/__mocks__/react-native/Libraries/ReactPrivate/ReactNativePrivateInterface.js

+11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* @flow strict-local
88
*/
99

10+
export opaque type PublicInstance = mixed;
11+
1012
module.exports = {
1113
get BatchedBridge() {
1214
return require('./BatchedBridge.js');
@@ -44,4 +46,13 @@ module.exports = {
4446
get RawEventEmitter() {
4547
return require('./RawEventEmitter').default;
4648
},
49+
get getNativeTagFromPublicInstance() {
50+
return require('./getNativeTagFromPublicInstance').default;
51+
},
52+
get getNodeFromPublicInstance() {
53+
return require('./getNodeFromPublicInstance').default;
54+
},
55+
get createPublicInstance() {
56+
return require('./createPublicInstance').default;
57+
},
4758
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
* @flow strict
8+
*/
9+
10+
import type {PublicInstance} from './ReactNativePrivateInterface';
11+
12+
export default function createPublicInstance(
13+
tag: number,
14+
viewConfig: mixed,
15+
internalInstanceHandle: mixed,
16+
): PublicInstance {
17+
return {
18+
__nativeTag: tag,
19+
__internalInstanceHandle: internalInstanceHandle,
20+
};
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
* @flow strict
8+
*/
9+
10+
import type {PublicInstance} from './ReactNativePrivateInterface';
11+
12+
export default function getNativeTagFromPublicInstance(
13+
publicInstance: PublicInstance,
14+
) {
15+
return publicInstance.__nativeTag;
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
* @flow strict
8+
*/
9+
10+
import type {PublicInstance} from './ReactNativePrivateInterface';
11+
12+
import {getNodeFromInternalInstanceHandle} from '../../../../ReactNativePublicCompat';
13+
14+
export default function getNodeFromPublicInstance(
15+
publicInstance: PublicInstance,
16+
) {
17+
return getNodeFromInternalInstanceHandle(
18+
publicInstance.__internalInstanceHandle,
19+
);
20+
}

Diff for: packages/react-native-renderer/src/__tests__/ReactFabric-test.internal.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ describe('ReactFabric', () => {
4343
require('react-native/Libraries/ReactPrivate/ReactNativePrivateInterface')
4444
.ReactNativeViewConfigRegistry.register;
4545
getNativeTagFromPublicInstance =
46-
require('../ReactFabricPublicInstanceUtils').getNativeTagFromPublicInstance;
46+
require('react-native/Libraries/ReactPrivate/ReactNativePrivateInterface').getNativeTagFromPublicInstance;
4747
getNodeFromPublicInstance =
48-
require('../ReactFabricPublicInstanceUtils').getNodeFromPublicInstance;
48+
require('react-native/Libraries/ReactPrivate/ReactNativePrivateInterface').getNodeFromPublicInstance;
4949

5050
act = require('internal-test-utils').act;
5151
});

Diff for: packages/react-native-renderer/src/__tests__/ReactFabricAndNative-test.internal.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('created with ReactFabric called with ReactNative', () => {
3737
require('react-native/Libraries/ReactPrivate/ReactNativePrivateInterface')
3838
.ReactNativeViewConfigRegistry.register;
3939
getNativeTagFromPublicInstance =
40-
require('../ReactFabricPublicInstanceUtils').getNativeTagFromPublicInstance;
40+
require('react-native/Libraries/ReactPrivate/ReactNativePrivateInterface').getNativeTagFromPublicInstance;
4141
});
4242

4343
it('find Fabric instances with the RN renderer', () => {

0 commit comments

Comments
 (0)