Skip to content

Commit 2bd1868

Browse files
huntiemeta-codesync[bot]
authored andcommitted
Align to canonical *Instance types in component ref annotations
Summary: Update all ref parameters, ref-returning props, and `useRef` calls in built-in components to use the canonical `*Instance` types introduced in the previous diff. #### Changes - Replace inline `React.ElementRef<typeof NativeComponent>` patterns with the canonical `*Instance` type in each component declaration and its inline destructuring type. - Replace `useRef<React.ElementRef<...>>` with `useRef<*Instance>` (Pressable, Switch). - Replace `hostRef` prop types with the component `*Instance` type (TouchableHighlight, TouchableOpacity). - Update `ModalBaseProps.modalRef` and `ModalRefProps.ref` from deprecated `PublicModalInstance` to `ModalInstance`. #### Removed aliases - `ButtonRef` (Button.js) — inlined into `ButtonInstance` - `SwitchRef` (Switch.js) — replaced by `SwitchInstance` - `TextForwardRef` (Text.js) — replaced by `TextInstance` - `type Instance` (Pressable.js) — replaced by `PressableInstance` - `HostComponent<empty>` import (ActivityIndicator.js) — no longer needed - `ProgressBarAndroidNativeComponentType` typeof import (ProgressBarAndroid.js) — no longer needed Changelog: [Internal] Differential Revision: D106144380
1 parent 55a3c1c commit 2bd1868

15 files changed

Lines changed: 95 additions & 197 deletions

File tree

packages/react-native/Libraries/Components/ActivityIndicator/ActivityIndicator.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
*/
1010

1111
'use strict';
12-
import type {HostComponent} from '../../../src/private/types/HostComponent';
1312
import type {HostInstance} from '../../../src/private/types/HostInstance';
1413
import type {ViewProps} from '../View/ViewPropTypes';
1514

@@ -65,7 +64,7 @@ export type ActivityIndicatorProps = Readonly<{
6564
}>;
6665

6766
const ActivityIndicator: component(
68-
ref?: React.RefSetter<HostComponent<empty>>,
67+
ref?: React.RefSetter<ActivityIndicatorInstance>,
6968
...props: ActivityIndicatorProps
7069
) = ({
7170
ref: forwardedRef,

packages/react-native/Libraries/Components/Button.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,14 +284,18 @@ const NativeTouchable:
284284
| typeof TouchableOpacity =
285285
Platform.OS === 'android' ? TouchableNativeFeedback : TouchableOpacity;
286286

287-
type ButtonRef = React.ElementRef<typeof NativeTouchable>;
288-
289-
export type ButtonInstance = ButtonRef;
287+
export type ButtonInstance = React.ElementRef<typeof NativeTouchable>;
290288

291289
const Button: component(
292-
ref?: React.RefSetter<ButtonRef>,
290+
ref?: React.RefSetter<ButtonInstance>,
293291
...props: ButtonProps
294-
) = ({ref, ...props}: {ref?: React.RefSetter<ButtonRef>, ...ButtonProps}) => {
292+
) = ({
293+
ref,
294+
...props
295+
}: {
296+
ref?: React.RefSetter<ButtonInstance>,
297+
...ButtonProps,
298+
}) => {
295299
const {
296300
accessibilityLabel,
297301
accessibilityState,

packages/react-native/Libraries/Components/Pressable/Pressable.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,6 @@ export type PressableProps = Readonly<{
168168
...PressableBaseProps,
169169
}>;
170170

171-
type Instance = React.ElementRef<typeof View>;
172-
173171
/**
174172
* Component used to build display components that should respond to whether the
175173
* component is currently pressed or not.
@@ -178,7 +176,7 @@ function Pressable({
178176
ref: forwardedRef,
179177
...props
180178
}: {
181-
ref?: React.RefSetter<Instance>,
179+
ref?: React.RefSetter<PressableInstance>,
182180
...PressableProps,
183181
}): React.Node {
184182
const {
@@ -218,7 +216,7 @@ function Pressable({
218216
...restProps
219217
} = props;
220218

221-
const viewRef = useRef<Instance | null>(null);
219+
const viewRef = useRef<PressableInstance | null>(null);
222220
const mergedRef = useMergeRefs(forwardedRef, viewRef);
223221

224222
const android_rippleConfig = useAndroidRippleForView(android_ripple, viewRef);
@@ -357,6 +355,6 @@ const MemoedPressable = memo(Pressable);
357355
MemoedPressable.displayName = 'Pressable';
358356

359357
export default MemoedPressable as component(
360-
ref?: React.RefSetter<React.ElementRef<typeof View>>,
358+
ref?: React.RefSetter<PressableInstance>,
361359
...props: PressableProps
362360
);

packages/react-native/Libraries/Components/ProgressBarAndroid/ProgressBarAndroid.android.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ export type {ProgressBarAndroidProps};
4343
* ```
4444
*/
4545
const ProgressBarAndroid: component(
46-
ref?: React.RefSetter<
47-
React.ElementRef<typeof ProgressBarAndroidNativeComponent>,
48-
>,
46+
ref?: React.RefSetter<ProgressBarAndroidInstance>,
4947
...props: ProgressBarAndroidProps
5048
) = function ProgressBarAndroid({
5149
ref: forwardedRef,
@@ -55,9 +53,7 @@ const ProgressBarAndroid: component(
5553
animating = true,
5654
...restProps
5755
}: {
58-
ref?: React.RefSetter<
59-
React.ElementRef<typeof ProgressBarAndroidNativeComponent>,
60-
>,
56+
ref?: React.RefSetter<ProgressBarAndroidInstance>,
6157
...ProgressBarAndroidProps,
6258
}) {
6359
return (

packages/react-native/Libraries/Components/ProgressBarAndroid/ProgressBarAndroid.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
'use strict';
1212

1313
import type {HostInstance} from '../../../src/private/types/HostInstance';
14-
import typeof ProgressBarAndroidNativeComponentType from './ProgressBarAndroidNativeComponent';
1514
import type {ProgressBarAndroidProps} from './ProgressBarAndroidTypes';
1615

1716
import Platform from '../../Utilities/Platform';
@@ -33,9 +32,7 @@ type Omit<T, K> = T extends any ? Pick<T, Exclude<keyof T, K>> : T;
3332
* @deprecated
3433
*/
3534
let ProgressBarAndroid: component(
36-
ref?: React.RefSetter<
37-
React.ElementRef<ProgressBarAndroidNativeComponentType>,
38-
>,
35+
ref?: React.RefSetter<ProgressBarAndroidInstance>,
3936
...props: Omit<ProgressBarAndroidProps, empty>
4037
);
4138

packages/react-native/Libraries/Components/SafeAreaView/SafeAreaView.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import * as React from 'react';
2828
* @deprecated Use `react-native-safe-area-context` instead. This component will be removed in a future release.
2929
*/
3030
const SafeAreaView: component(
31-
ref?: React.RefSetter<React.ElementRef<typeof View>>,
31+
ref?: React.RefSetter<SafeAreaViewInstance>,
3232
...props: ViewProps
3333
) = Platform.select({
3434
ios: require('./RCTSafeAreaViewNativeComponent').default,

packages/react-native/Libraries/Components/Switch/Switch.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,6 @@ export type SwitchProps = Readonly<{
122122
const returnsFalse = () => false;
123123
const returnsTrue = () => true;
124124

125-
type SwitchRef = React.ElementRef<
126-
typeof SwitchNativeComponent | typeof AndroidSwitchNativeComponent,
127-
>;
128-
129125
/**
130126
Renders a boolean input.
131127
@@ -168,13 +164,13 @@ type SwitchRef = React.ElementRef<
168164
```
169165
*/
170166
const Switch: component(
171-
ref?: React.RefSetter<SwitchRef>,
167+
ref?: React.RefSetter<SwitchInstance>,
172168
...props: SwitchProps
173169
) = function Switch({
174170
ref: forwardedRef,
175171
...props
176172
}: {
177-
ref?: React.RefSetter<SwitchRef>,
173+
ref?: React.RefSetter<SwitchInstance>,
178174
...SwitchProps,
179175
}): React.Node {
180176
const {
@@ -191,9 +187,7 @@ const Switch: component(
191187
const trackColorForFalse = trackColor?.false;
192188
const trackColorForTrue = trackColor?.true;
193189

194-
const nativeSwitchRef = useRef<React.ElementRef<
195-
typeof SwitchNativeComponent | typeof AndroidSwitchNativeComponent,
196-
> | null>(null);
190+
const nativeSwitchRef = useRef<SwitchInstance | null>(null);
197191

198192
const ref = useMergeRefs(nativeSwitchRef, forwardedRef);
199193

packages/react-native/Libraries/Components/Touchable/TouchableHighlight.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ type TouchableHighlightBaseProps = Readonly<{
6363
onHideUnderlay?: ?() => void,
6464
testOnly_pressed?: ?boolean,
6565

66-
hostRef?: React.RefSetter<React.ElementRef<typeof View>>,
66+
hostRef?: React.RefSetter<TouchableHighlightInstance>,
6767
}>;
6868

6969
/** @build-types emit-as-interface Uniwind compatibility */
@@ -415,13 +415,13 @@ class TouchableHighlightImpl extends React.Component<
415415
}
416416

417417
const TouchableHighlight: component(
418-
ref?: React.RefSetter<React.ElementRef<typeof View>>,
418+
ref?: React.RefSetter<TouchableHighlightInstance>,
419419
...props: Readonly<Omit<TouchableHighlightProps, 'hostRef'>>
420420
) = ({
421421
ref: hostRef,
422422
...props
423423
}: {
424-
ref?: React.RefSetter<React.ElementRef<typeof View>>,
424+
ref?: React.RefSetter<TouchableHighlightInstance>,
425425
...Readonly<Omit<TouchableHighlightProps, 'hostRef'>>,
426426
}) => <TouchableHighlightImpl {...props} hostRef={hostRef} />;
427427

packages/react-native/Libraries/Components/Touchable/TouchableOpacity.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ type TouchableOpacityBaseProps = Readonly<{
7777
activeOpacity?: ?number,
7878
style?: ?Animated.WithAnimatedValue<ViewStyleProp>,
7979

80-
hostRef?: ?React.RefSetter<React.ElementRef<typeof Animated.View>>,
80+
hostRef?: ?React.RefSetter<TouchableOpacityInstance>,
8181
}>;
8282

8383
export type TouchableOpacityProps = Readonly<{
@@ -381,13 +381,13 @@ class TouchableOpacity extends React.Component<
381381
}
382382

383383
const Touchable: component(
384-
ref?: React.RefSetter<React.ElementRef<typeof Animated.View>>,
384+
ref?: React.RefSetter<TouchableOpacityInstance>,
385385
...props: TouchableOpacityProps
386386
) = ({
387387
ref,
388388
...props
389389
}: {
390-
ref?: React.RefSetter<React.ElementRef<typeof Animated.View>>,
390+
ref?: React.RefSetter<TouchableOpacityInstance>,
391391
...TouchableOpacityProps,
392392
}) => <TouchableOpacity {...props} hostRef={ref} />;
393393

packages/react-native/Libraries/Components/View/View.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ import {use} from 'react';
2525
*
2626
* @see https://reactnative.dev/docs/view
2727
*/
28-
component View(
29-
ref?: React.RefSetter<React.ElementRef<typeof ViewNativeComponent>>,
30-
...props: ViewProps
31-
) {
28+
component View(ref?: React.RefSetter<ViewInstance>, ...props: ViewProps) {
3229
const hasTextAncestor = use(TextAncestorContext);
3330

3431
const {

0 commit comments

Comments
 (0)