Skip to content

Commit b4ac37d

Browse files
sbalagan22claude
andcommitted
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<typeof View>` 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<typeof View>`, checked against both the legacy (types/) and generated strict (types_generated/) definitions. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent d06174f commit b4ac37d

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
*/
9+
10+
import * as React from 'react';
11+
import {View} from 'react-native';
12+
13+
// A ref to a host component resolves to its imperative handle, which exposes
14+
// the native measurement and mutation methods. `React.ComponentRef<typeof View>`
15+
// is the pattern that resolves to that handle under both the legacy and the
16+
// strict public types; the `View` component type itself is not the handle.
17+
type ViewHandle = React.ComponentRef<typeof View>;
18+
19+
export function ViewImperativeHandle() {
20+
const viewRef = React.useRef<ViewHandle>(null);
21+
const targetRef = React.useRef<ViewHandle>(null);
22+
23+
const measure = () => {
24+
const view = viewRef.current;
25+
const target = targetRef.current;
26+
if (view == null || target == null) {
27+
return;
28+
}
29+
30+
view.measure(
31+
(
32+
x: number,
33+
y: number,
34+
width: number,
35+
height: number,
36+
pageX: number,
37+
pageY: number,
38+
) => {},
39+
);
40+
41+
view.measureInWindow(
42+
(x: number, y: number, width: number, height: number) => {},
43+
);
44+
45+
view.measureLayout(
46+
target,
47+
(left: number, top: number, width: number, height: number) => {},
48+
() => {},
49+
);
50+
51+
view.setNativeProps({});
52+
};
53+
54+
return (
55+
<View ref={viewRef}>
56+
<View ref={targetRef} />
57+
</View>
58+
);
59+
}

0 commit comments

Comments
 (0)