From 886ab8175610407409753384a7fbd21153690ea0 Mon Sep 17 00:00:00 2001 From: Sukhman Date: Tue, 7 Jul 2026 12:02:43 -0400 Subject: [PATCH] Add type-test for View imperative handle methods A ref to a host component resolves to its imperative handle (ReactNativeElement under the strict public types), which is where the native measurement and mutation methods live. The View component type itself is not that handle, so `React.ComponentRef` is the type that resolves to it under both the legacy and the strict public types. The existing type-tests exercise measure, measureInWindow, and setNativeProps but never measureLayout, so the handle contract had a coverage gap. Add a focused type-test that pins measure, measureInWindow, measureLayout, and setNativeProps on `React.ComponentRef`, checked against both the legacy (types/) and generated strict (types_generated/) definitions. --- .../__typetests__/component-refs.tsx | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 packages/react-native/__typetests__/component-refs.tsx diff --git a/packages/react-native/__typetests__/component-refs.tsx b/packages/react-native/__typetests__/component-refs.tsx new file mode 100644 index 00000000000..15099abadb3 --- /dev/null +++ b/packages/react-native/__typetests__/component-refs.tsx @@ -0,0 +1,59 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + */ + +import * as React from 'react'; +import {View} from 'react-native'; + +// A ref to a host component resolves to its imperative handle, which exposes +// the native measurement and mutation methods. `React.ComponentRef` +// is the pattern that resolves to that handle under both the legacy and the +// strict public types; the `View` component type itself is not the handle. +type ViewHandle = React.ComponentRef; + +export function ViewImperativeHandle() { + const viewRef = React.useRef(null); + const targetRef = React.useRef(null); + + const measure = () => { + const view = viewRef.current; + const target = targetRef.current; + if (view == null || target == null) { + return; + } + + view.measure( + ( + x: number, + y: number, + width: number, + height: number, + pageX: number, + pageY: number, + ) => {}, + ); + + view.measureInWindow( + (x: number, y: number, width: number, height: number) => {}, + ); + + view.measureLayout( + target, + (left: number, top: number, width: number, height: number) => {}, + () => {}, + ); + + view.setNativeProps({}); + }; + + return ( + + + + ); +}