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<HostComponent<T>>`. Since `React.ElementRef<HostComponent<T>>` is typed as `React.AbstractComponent<T, NativeMethods>`, that means `React.ElementRef<HostComponent<T>>` is equivalent to `NativeMethods` which is equivalent to `HostInstance`. ## How did you test this change? ``` $ yarn $ yarn flow fabric ```
Timothy Yung committed
Oct 1, 2024 at 17:25 UTC
459fd418cfbd1f2f1be58efd8c89a0e0ecfb6d44
1 file changed
+12
-17
packages/react-native-renderer/src/ReactNativeTypes.js
+12
-17
@@ -112,31 +112,32 @@ export interface INativeMethods {
112
measure(callback: MeasureOnSuccessCallback): void;
113
measureInWindow(callback: MeasureInWindowOnSuccessCallback): void;
114
measureLayout(
115
- relativeToNativeNode: number | ElementRef<HostComponent<mixed>>,
115
+ relativeToNativeNode: number | HostInstance,
116
onSuccess: MeasureLayoutOnSuccessCallback,
117
onFail?: () => void,
118
): void;
119
setNativeProps(nativeProps: {...}): void;
120
}
121
122
-export type NativeMethods = $ReadOnly<{|
122
+export type NativeMethods = $ReadOnly<{
123
blur(): void,
124
focus(): void,
125
measure(callback: MeasureOnSuccessCallback): void,
126
measureInWindow(callback: MeasureInWindowOnSuccessCallback): void,
127
measureLayout(
128
- relativeToNativeNode: number | ElementRef<HostComponent<mixed>>,
128
+ relativeToNativeNode: number | HostInstance,
129
onSuccess: MeasureLayoutOnSuccessCallback,
130
onFail?: () => void,
131
): void,
132
setNativeProps(nativeProps: {...}): void,
133
-|}>;
133
+}>;
134
135
// This validates that INativeMethods and NativeMethods stay in sync using Flow!
136
declare const ensureNativeMethodsAreSynced: NativeMethods;
137
(ensureNativeMethodsAreSynced: INativeMethods);
138
139
-export type HostComponent<T> = AbstractComponent<T, $ReadOnly<NativeMethods>>;
139
+export type HostInstance = NativeMethods;
140
+export type HostComponent<Config> = AbstractComponent<Config, HostInstance>;
141
142
type SecretInternalsType = {
143
computeComponentStackForErrorReporting(tag: number): string,
@@ -209,7 +210,7 @@ export type RenderRootOptions = {
210
export type ReactNativeType = {
211
findHostInstance_DEPRECATED<TElementType: ElementType>(
212
componentOrHandle: ?(ElementRef<TElementType> | number),
212
- ): ?ElementRef<HostComponent<mixed>>,
213
+ ): ?HostInstance,
214
findNodeHandle<TElementType: ElementType>(
215
componentOrHandle: ?(ElementRef<TElementType> | number),
216
): ?number,
@@ -218,14 +219,11 @@ export type ReactNativeType = {
219
child: PublicInstance | HostComponent<mixed>,
220
): boolean,
221
dispatchCommand(
221
- handle: ElementRef<HostComponent<mixed>>,
222
+ handle: HostInstance,
223
command: string,
224
args: Array<mixed>,
225
): void,
225
- sendAccessibilityEvent(
226
- handle: ElementRef<HostComponent<mixed>>,
227
- eventType: string,
228
- ): void,
226
+ sendAccessibilityEvent(handle: HostInstance, eventType: string): void,
227
render(
228
element: MixedElement,
229
containerTag: number,
@@ -247,20 +245,17 @@ type PublicTextInstance = mixed;
245
export type ReactFabricType = {
246
findHostInstance_DEPRECATED<TElementType: ElementType>(
247
componentOrHandle: ?(ElementRef<TElementType> | number),
250
- ): ?ElementRef<HostComponent<mixed>>,
248
+ ): ?HostInstance,
249
findNodeHandle<TElementType: ElementType>(
250
componentOrHandle: ?(ElementRef<TElementType> | number),
251
): ?number,
252
dispatchCommand(
255
- handle: ElementRef<HostComponent<mixed>>,
253
+ handle: HostInstance,
254
command: string,
255
args: Array<mixed>,
256
): void,
257
isChildPublicInstance(parent: PublicInstance, child: PublicInstance): boolean,
260
- sendAccessibilityEvent(
261
- handle: ElementRef<HostComponent<mixed>>,
262
- eventType: string,
263
- ): void,
258
+ sendAccessibilityEvent(handle: HostInstance, eventType: string): void,
259
render(
260
element: MixedElement,
261
containerTag: number,