From 459fd418cfbd1f2f1be58efd8c89a0e0ecfb6d44 Mon Sep 17 00:00:00 2001 From: Timothy Yung Date: Tue, 1 Oct 2024 17:25:59 -0700 Subject: [PATCH] Define `HostInstance` type for React Native (#31101) ## Summary Creates a new `HostInstance` type for React Native, to more accurately capture the intent most developers have when using the `NativeMethods` type or `React.ElementRef>`. Since `React.ElementRef>` is typed as `React.AbstractComponent`, that means `React.ElementRef>` is equivalent to `NativeMethods` which is equivalent to `HostInstance`. ## How did you test this change? ``` $ yarn $ yarn flow fabric ``` --- .../src/ReactNativeTypes.js | 29 ++++++++----------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/packages/react-native-renderer/src/ReactNativeTypes.js b/packages/react-native-renderer/src/ReactNativeTypes.js index 9692a1256a..03c03cfba0 100644 --- a/packages/react-native-renderer/src/ReactNativeTypes.js +++ b/packages/react-native-renderer/src/ReactNativeTypes.js @@ -112,31 +112,32 @@ export interface INativeMethods { measure(callback: MeasureOnSuccessCallback): void; measureInWindow(callback: MeasureInWindowOnSuccessCallback): void; measureLayout( - relativeToNativeNode: number | ElementRef>, + relativeToNativeNode: number | HostInstance, onSuccess: MeasureLayoutOnSuccessCallback, onFail?: () => void, ): void; setNativeProps(nativeProps: {...}): void; } -export type NativeMethods = $ReadOnly<{| +export type NativeMethods = $ReadOnly<{ blur(): void, focus(): void, measure(callback: MeasureOnSuccessCallback): void, measureInWindow(callback: MeasureInWindowOnSuccessCallback): void, measureLayout( - relativeToNativeNode: number | ElementRef>, + relativeToNativeNode: number | HostInstance, onSuccess: MeasureLayoutOnSuccessCallback, onFail?: () => void, ): void, setNativeProps(nativeProps: {...}): void, -|}>; +}>; // This validates that INativeMethods and NativeMethods stay in sync using Flow! declare const ensureNativeMethodsAreSynced: NativeMethods; (ensureNativeMethodsAreSynced: INativeMethods); -export type HostComponent = AbstractComponent>; +export type HostInstance = NativeMethods; +export type HostComponent = AbstractComponent; type SecretInternalsType = { computeComponentStackForErrorReporting(tag: number): string, @@ -209,7 +210,7 @@ export type RenderRootOptions = { export type ReactNativeType = { findHostInstance_DEPRECATED( componentOrHandle: ?(ElementRef | number), - ): ?ElementRef>, + ): ?HostInstance, findNodeHandle( componentOrHandle: ?(ElementRef | number), ): ?number, @@ -218,14 +219,11 @@ export type ReactNativeType = { child: PublicInstance | HostComponent, ): boolean, dispatchCommand( - handle: ElementRef>, + handle: HostInstance, command: string, args: Array, ): void, - sendAccessibilityEvent( - handle: ElementRef>, - eventType: string, - ): void, + sendAccessibilityEvent(handle: HostInstance, eventType: string): void, render( element: MixedElement, containerTag: number, @@ -247,20 +245,17 @@ type PublicTextInstance = mixed; export type ReactFabricType = { findHostInstance_DEPRECATED( componentOrHandle: ?(ElementRef | number), - ): ?ElementRef>, + ): ?HostInstance, findNodeHandle( componentOrHandle: ?(ElementRef | number), ): ?number, dispatchCommand( - handle: ElementRef>, + handle: HostInstance, command: string, args: Array, ): void, isChildPublicInstance(parent: PublicInstance, child: PublicInstance): boolean, - sendAccessibilityEvent( - handle: ElementRef>, - eventType: string, - ): void, + sendAccessibilityEvent(handle: HostInstance, eventType: string): void, render( element: MixedElement, containerTag: number,