diff --git a/packages/react-devtools-shared/src/backend/fiber/renderer.js b/packages/react-devtools-shared/src/backend/fiber/renderer.js index d93e713911561..22a7afcc4c632 100644 --- a/packages/react-devtools-shared/src/backend/fiber/renderer.js +++ b/packages/react-devtools-shared/src/backend/fiber/renderer.js @@ -763,16 +763,30 @@ const hostResourceToDevToolsInstanceMap: Map< Set, > = new Map(); +// Ideally, this should be injected from Reconciler config function getPublicInstance(instance: HostInstance): HostInstance { // Typically the PublicInstance and HostInstance is the same thing but not in Fabric. // So we need to detect this and use that as the public instance. - return typeof instance === 'object' && - instance !== null && - typeof instance.canonical === 'object' - ? (instance.canonical: any) - : typeof instance._nativeTag === 'number' - ? instance._nativeTag - : instance; + + // React Native. Modern. Fabric. + if (typeof instance === 'object' && instance !== null) { + if (typeof instance.canonical === 'object' && instance.canonical !== null) { + if ( + typeof instance.canonical.publicInstance === 'object' && + instance.canonical.publicInstance !== null + ) { + return instance.canonical.publicInstance; + } + } + + // React Native. Legacy. Paper. + if (typeof instance._nativeTag === 'number') { + return instance._nativeTag; + } + } + + // React Web. Usually a DOM element. + return instance; } function aquireHostInstance( diff --git a/packages/react-native-renderer/src/ReactNativeFiberInspector.js b/packages/react-native-renderer/src/ReactNativeFiberInspector.js index b0859471c463e..d0423f1d48cfa 100644 --- a/packages/react-native-renderer/src/ReactNativeFiberInspector.js +++ b/packages/react-native-renderer/src/ReactNativeFiberInspector.js @@ -209,6 +209,8 @@ function getInspectorDataForViewAtPoint( closestInstance = internalInstanceHandle.stateNode.canonical.internalInstanceHandle; + const closestPublicInstance = + internalInstanceHandle.stateNode.canonical.publicInstance; // Note: this is deprecated and we want to remove it ASAP. Keeping it here for React DevTools compatibility for now. const nativeViewTag = @@ -224,6 +226,7 @@ function getInspectorDataForViewAtPoint( pointerY: locationY, frame: {left: pageX, top: pageY, width, height}, touchedViewTag: nativeViewTag, + closestPublicInstance, }); }, ); @@ -243,6 +246,7 @@ function getInspectorDataForViewAtPoint( pointerY: locationY, frame: {left, top, width, height}, touchedViewTag: nativeViewTag, + closestPublicInstance: nativeViewTag, }); }, ); diff --git a/packages/react-native-renderer/src/ReactNativeTypes.js b/packages/react-native-renderer/src/ReactNativeTypes.js index 000ea0d0f766d..9692a1256acff 100644 --- a/packages/react-native-renderer/src/ReactNativeTypes.js +++ b/packages/react-native-renderer/src/ReactNativeTypes.js @@ -179,6 +179,7 @@ export type TouchedViewDataAtPoint = $ReadOnly<{ width: number, height: number, }>, + closestPublicInstance?: PublicInstance, ...InspectorData, }>;