Skip to content

Commit

Permalink
refactor[AppContainer]: remove innerView ref from state (#41613)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #41613

Changelog: [Internal]

We were keeping ref in the AppContainer's state before migrating these components to functions.

We actually need a real ref here, so we can use `useImperativeHandle` on it later. These refs will be passed as an argument for DebuggingRegistry subscription, whichi will call them on all necessary AppContainers to highlight required components.

Reviewed By: javache

Differential Revision: D51536772

fbshipit-source-id: d49035874ce3c9b1acf08d5ab666886f68e6f40e
  • Loading branch information
hoxyq authored and facebook-github-bot committed Nov 23, 2023
1 parent ed1056e commit b71bb81
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
7 changes: 3 additions & 4 deletions packages/react-native/Libraries/Inspector/Inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import type {
} from '../Renderer/shims/ReactNativeTypes';
import type {ViewStyleProp} from '../StyleSheet/StyleSheet';
import type {ReactDevToolsAgent} from '../Types/ReactDevToolsTypes';
import type {HostRef} from './getInspectorDataForViewAtPoint';

const View = require('../Components/View/View');
const PressabilityDebug = require('../Pressability/PressabilityDebug');
Expand Down Expand Up @@ -47,13 +46,13 @@ export type InspectedElement = $ReadOnly<{
export type ElementsHierarchy = InspectorData['hierarchy'];

type Props = {
inspectedView: ?HostRef,
inspectedViewRef: React.RefObject<React.ElementRef<typeof View> | null>,
onRequestRerenderApp: () => void,
reactDevToolsAgent?: ReactDevToolsAgent,
};

function Inspector({
inspectedView,
inspectedViewRef,
onRequestRerenderApp,
reactDevToolsAgent,
}: Props): React.Node {
Expand Down Expand Up @@ -126,7 +125,7 @@ function Inspector({
};

getInspectorDataForViewAtPoint(
inspectedView,
inspectedViewRef.current,
locationX,
locationY,
viewData => {
Expand Down
42 changes: 23 additions & 19 deletions packages/react-native/Libraries/Inspector/ReactDevToolsOverlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import type {
InstanceFromReactDevTools,
ReactDevToolsAgent,
} from '../Types/ReactDevToolsTypes';
import type {HostRef} from './getInspectorDataForViewAtPoint';
import type {InspectedElement} from './Inspector';

import View from '../Components/View/View';
Expand All @@ -30,12 +29,12 @@ const getInspectorDataForViewAtPoint = require('./getInspectorDataForViewAtPoint
const {useEffect, useState, useCallback} = React;

type Props = {
inspectedView: ?HostRef,
inspectedViewRef: React.RefObject<React.ElementRef<typeof View> | null>,
reactDevToolsAgent: ReactDevToolsAgent,
};

export default function ReactDevToolsOverlay({
inspectedView,
inspectedViewRef,
reactDevToolsAgent,
}: Props): React.Node {
const [inspected, setInspected] = useState<?InspectedElement>(null);
Expand Down Expand Up @@ -125,24 +124,29 @@ export default function ReactDevToolsOverlay({

const findViewForLocation = useCallback(
(x: number, y: number) => {
getInspectorDataForViewAtPoint(inspectedView, x, y, viewData => {
const {touchedViewTag, closestInstance, frame} = viewData;
if (closestInstance != null || touchedViewTag != null) {
// We call `selectNode` for both non-fabric(viewTag) and fabric(instance),
// this makes sure it works for both architectures.
reactDevToolsAgent.selectNode(findNodeHandle(touchedViewTag));
if (closestInstance != null) {
reactDevToolsAgent.selectNode(closestInstance);
getInspectorDataForViewAtPoint(
inspectedViewRef.current,
x,
y,
viewData => {
const {touchedViewTag, closestInstance, frame} = viewData;
if (closestInstance != null || touchedViewTag != null) {
// We call `selectNode` for both non-fabric(viewTag) and fabric(instance),
// this makes sure it works for both architectures.
reactDevToolsAgent.selectNode(findNodeHandle(touchedViewTag));
if (closestInstance != null) {
reactDevToolsAgent.selectNode(closestInstance);
}
setInspected({
frame,
});
return true;
}
setInspected({
frame,
});
return true;
}
return false;
});
return false;
},
);
},
[inspectedView, reactDevToolsAgent],
[inspectedViewRef, reactDevToolsAgent],
);

const stopInspecting = useCallback(() => {
Expand Down
17 changes: 7 additions & 10 deletions packages/react-native/Libraries/ReactNative/AppContainer-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import type {Props} from './AppContainer';
import TraceUpdateOverlay from '../Components/TraceUpdateOverlay/TraceUpdateOverlay';
import ReactNativeStyleAttributes from '../Components/View/ReactNativeStyleAttributes';
import View from '../Components/View/View';
import ViewNativeComponent from '../Components/View/ViewNativeComponent';
import RCTDeviceEventEmitter from '../EventEmitter/RCTDeviceEventEmitter';
import ReactDevToolsOverlay from '../Inspector/ReactDevToolsOverlay';
import LogBoxNotificationContainer from '../LogBox/LogBoxNotificationContainer';
Expand All @@ -41,13 +40,13 @@ if (reactDevToolsHook) {
}

type InspectorDeferredProps = {
inspectedView: React.ElementRef<typeof ViewNativeComponent> | null,
inspectedViewRef: React.RefObject<React.ElementRef<typeof View> | null>,
onInspectedViewRerenderRequest: () => void,
reactDevToolsAgent?: ReactDevToolsAgent,
};

const InspectorDeferred = ({
inspectedView,
inspectedViewRef,
onInspectedViewRerenderRequest,
reactDevToolsAgent,
}: InspectorDeferredProps) => {
Expand All @@ -57,7 +56,7 @@ const InspectorDeferred = ({

return (
<Inspector
inspectedView={inspectedView}
inspectedViewRef={inspectedViewRef}
onRequestRerenderApp={onInspectedViewRerenderRequest}
reactDevToolsAgent={reactDevToolsAgent}
/>
Expand All @@ -74,9 +73,7 @@ const AppContainer = ({
showArchitectureIndicator,
WrapperComponent,
}: Props): React.Node => {
const [mainRef, setMainRef] = useState<React.ElementRef<
typeof ViewNativeComponent,
> | null>(null);
const innerViewRef = React.useRef<React.ElementRef<typeof View> | null>(null);

const [key, setKey] = useState(0);
const [shouldRenderInspector, setShouldRenderInspector] = useState(false);
Expand Down Expand Up @@ -118,7 +115,7 @@ const AppContainer = ({
pointerEvents="box-none"
key={key}
style={styles.container}
ref={setMainRef}>
ref={innerViewRef}>
{children}
</View>
);
Expand Down Expand Up @@ -149,14 +146,14 @@ const AppContainer = ({
)}
{reactDevToolsAgent != null && (
<ReactDevToolsOverlay
inspectedView={mainRef}
inspectedViewRef={innerViewRef}
reactDevToolsAgent={reactDevToolsAgent}
/>
)}

{shouldRenderInspector && (
<InspectorDeferred
inspectedView={mainRef}
inspectedViewRef={innerViewRef}
onInspectedViewRerenderRequest={onInspectedViewRerenderRequest}
reactDevToolsAgent={reactDevToolsAgent}
/>
Expand Down

0 comments on commit b71bb81

Please sign in to comment.