Skip to content

Commit 535b844

Browse files
huntiemeta-codesync[bot]
authored andcommitted
Update return type for getNativeScrollRef methods (#56718)
Summary: Pull Request resolved: #56718 Update the return type of `getNativeScrollRef` on `ScrollView`, `FlatList`, `SectionList` to be `PublicScrollViewInstance` (previously, a mix of `HostInstance` and `React.ElementRef<>` types which did not include the imperative methods of `ScrollView`). - This type extends `HostInstance & ScrollViewImperativeMethods`, and is what the `ScrollView` ref chain already returns at runtime. - The previous types were either too broad (`HostInstance`), wrong (union with `View`), or required `$FlowFixMe` suppressions. Also removes `ScrollViewNativeComponent` from the public API surface, since it is an internal implementation detail not intended for external use (equivalent props are on the pre-existing `ScrollViewBaseProps` type). Related to: - #52203 - #54735 Changelog: [General][Fixed] - **Strict TypeScript API**: Update `getNativeScrollRef` return type across ScrollView, FlatList, and SectionList Reviewed By: zeyap Differential Revision: D104223704 fbshipit-source-id: 7f44f91518d7f84d8a628e58095e616589a068a3
1 parent 5162816 commit 535b844

3 files changed

Lines changed: 20 additions & 94 deletions

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export interface ScrollViewImperativeMethods {
139139
+getScrollableNode: () => ?number;
140140
+getInnerViewNode: () => ?number;
141141
+getInnerViewRef: () => InnerViewInstance | null;
142-
+getNativeScrollRef: () => HostInstance | null;
142+
+getNativeScrollRef: () => PublicScrollViewInstance | null;
143143
+scrollTo: (
144144
options?: ScrollViewScrollToOptions | number,
145145
deprecatedX?: number,
@@ -872,6 +872,9 @@ class ScrollView extends React.Component<ScrollViewProps, ScrollViewState> {
872872

873873
getNativeScrollRef: ScrollViewImperativeMethods['getNativeScrollRef'] =
874874
() => {
875+
// Object.assign in _scrollView's mutator augments nativeInstance in place,
876+
// so it is already a PublicScrollViewInstance at runtime.
877+
// $FlowFixMe[incompatible-type]
875878
return this._scrollView.nativeInstance;
876879
};
877880

packages/react-native/Libraries/Lists/FlatList.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @format
99
*/
1010

11-
import typeof ScrollViewNativeComponent from '../Components/ScrollView/ScrollViewNativeComponent';
11+
import type {PublicScrollViewInstance} from '../Components/ScrollView/ScrollView';
1212
import type {ViewStyleProp} from '../StyleSheet/StyleSheet';
1313
import type {
1414
ListRenderItem,
@@ -397,12 +397,8 @@ class FlatList<ItemT = any> extends React.PureComponent<FlatListProps<ItemT>> {
397397
/**
398398
* Provides a reference to the underlying host component
399399
*/
400-
getNativeScrollRef():
401-
| ?React.ElementRef<typeof View>
402-
| ?React.ElementRef<ScrollViewNativeComponent> {
400+
getNativeScrollRef(): ?PublicScrollViewInstance {
403401
if (this._listRef) {
404-
/* $FlowFixMe[incompatible-return] Suppresses errors found when fixing
405-
* TextInput typing */
406402
return this._listRef.getScrollRef();
407403
}
408404
}

packages/react-native/ReactNativeApi.d.ts

Lines changed: 14 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<d6790c241d1bd46a735efba34eeb4742>>
7+
* @generated SignedSource<<82e0f2ccc327245285474ce136692c2d>>
88
*
99
* This file was generated by scripts/js-api/build-types/index.js.
1010
*/
@@ -90,7 +90,6 @@ declare const $$NativeDialogManagerAndroid: null | Spec | undefined
9090
declare const $$ProgressBarAndroidNativeComponent: HostComponent<AndroidProgressBarNativeProps>
9191
declare const $$ReactFabric: typeof ReactFabric_default
9292
declare const $$ScrollViewContext: typeof ScrollViewContext_default
93-
declare const $$ScrollViewNativeComponent: typeof ScrollViewNativeComponent_default
9493
declare const $$SwitchNativeComponent: ComponentType
9594
declare const $$ViewNativeComponent: typeof ViewNativeComponent_default
9695
declare const absoluteFill: AbsoluteFillStyle
@@ -400,7 +399,6 @@ declare const SafeAreaView_default: (
400399
) => React.ReactNode
401400
declare const ScrollView: typeof ScrollViewWrapper & ScrollViewComponentStatics
402401
declare const ScrollViewContext_default: React.Context<Value>
403-
declare const ScrollViewNativeComponent_default: HostComponent<ScrollViewNativeProps>
404402
declare const ScrollViewWrapper: (
405403
props: ScrollViewProps & {
406404
ref?: React.Ref<PublicScrollViewInstance>
@@ -1016,7 +1014,6 @@ declare type $$ProgressBarAndroidNativeComponent =
10161014
typeof $$ProgressBarAndroidNativeComponent
10171015
declare type $$ReactFabric = typeof $$ReactFabric
10181016
declare type $$ScrollViewContext = typeof $$ScrollViewContext
1019-
declare type $$ScrollViewNativeComponent = typeof $$ScrollViewNativeComponent
10201017
declare type $$SwitchNativeComponent = typeof $$SwitchNativeComponent
10211018
declare type $$ViewNativeComponent = typeof $$ViewNativeComponent
10221019
declare type absoluteFill = typeof absoluteFill
@@ -2334,9 +2331,7 @@ declare class FlatList<ItemT = any> extends React.PureComponent<
23342331
componentDidUpdate(prevProps: FlatListProps<ItemT>): void
23352332
constructor(props: FlatListProps<ItemT>)
23362333
flashScrollIndicators(): void
2337-
getNativeScrollRef():
2338-
| (null | React.ComponentRef<ScrollViewNativeComponent> | undefined)
2339-
| (null | React.ComponentRef<typeof View> | undefined)
2334+
getNativeScrollRef(): null | PublicScrollViewInstance | undefined
23402335
getScrollableNode(): any
23412336
getScrollResponder(): null | ScrollResponderType | undefined
23422337
recordInteraction(): void
@@ -4478,7 +4473,7 @@ declare interface ScrollViewImperativeMethods {
44784473
readonly flashScrollIndicators: () => void
44794474
readonly getInnerViewNode: () => number | undefined
44804475
readonly getInnerViewRef: () => InnerViewInstance | null
4481-
readonly getNativeScrollRef: () => HostInstance | null
4476+
readonly getNativeScrollRef: () => null | PublicScrollViewInstance
44824477
readonly getScrollableNode: () => number | undefined
44834478
readonly getScrollResponder: () => ScrollResponderType
44844479
readonly scrollResponderScrollNativeHandleToKeyboard: (
@@ -4505,74 +4500,6 @@ declare interface ScrollViewImperativeMethods {
45054500
options?: ScrollViewScrollToOptions | undefined,
45064501
) => void
45074502
}
4508-
declare type ScrollViewNativeComponent = typeof $$ScrollViewNativeComponent
4509-
declare type ScrollViewNativeProps = Readonly<
4510-
Omit<ViewProps, "onResponderGrant"> & {
4511-
alwaysBounceHorizontal?: boolean
4512-
alwaysBounceVertical?: boolean
4513-
automaticallyAdjustContentInsets?: boolean
4514-
automaticallyAdjustKeyboardInsets?: boolean
4515-
automaticallyAdjustsScrollIndicatorInsets?: boolean
4516-
bounces?: boolean
4517-
bouncesZoom?: boolean
4518-
canCancelContentTouches?: boolean
4519-
centerContent?: boolean
4520-
contentInset?: EdgeInsetsProp
4521-
contentInsetAdjustmentBehavior?:
4522-
| "always"
4523-
| "automatic"
4524-
| "never"
4525-
| "scrollableAxes"
4526-
contentOffset?: PointProp
4527-
decelerationRate?: "fast" | "normal" | number
4528-
directionalLockEnabled?: boolean
4529-
disableIntervalMomentum?: boolean
4530-
endFillColor?: ColorValue
4531-
fadingEdgeLength?:
4532-
| (number | undefined)
4533-
| {
4534-
end: number
4535-
start: number
4536-
}
4537-
indicatorStyle?: "black" | "default" | "white"
4538-
isInvertedVirtualizedList?: boolean
4539-
keyboardDismissMode?: "interactive" | "none" | "on-drag"
4540-
maintainVisibleContentPosition?: {
4541-
readonly autoscrollToTopThreshold?: number
4542-
readonly minIndexForVisible: number
4543-
}
4544-
maximumZoomScale?: number
4545-
minimumZoomScale?: number
4546-
nestedScrollEnabled?: boolean
4547-
onMomentumScrollBegin?: (event: ScrollEvent) => void
4548-
onMomentumScrollEnd?: (event: ScrollEvent) => void
4549-
onResponderGrant?: (e: GestureResponderEvent) => boolean | void
4550-
onScroll?: (event: ScrollEvent) => void
4551-
onScrollBeginDrag?: (event: ScrollEvent) => void
4552-
onScrollEndDrag?: (event: ScrollEvent) => void
4553-
overScrollMode?: "always" | "auto" | "never"
4554-
pagingEnabled?: boolean
4555-
persistentScrollbar?: boolean
4556-
pinchGestureEnabled?: boolean
4557-
scrollEnabled?: boolean
4558-
scrollEventThrottle?: number
4559-
scrollIndicatorInsets?: EdgeInsetsProp
4560-
scrollPerfTag?: string
4561-
scrollsChildToFocus?: boolean
4562-
scrollsToTop?: boolean
4563-
scrollToOverflowEnabled?: boolean
4564-
sendMomentumEvents?: boolean
4565-
showsHorizontalScrollIndicator?: boolean
4566-
showsVerticalScrollIndicator?: boolean
4567-
snapToAlignment?: "center" | "end" | "start"
4568-
snapToEnd?: boolean
4569-
snapToInterval?: number
4570-
snapToOffsets?: ReadonlyArray<number>
4571-
snapToStart?: boolean
4572-
zoomScale?: number
4573-
onScrollToTop?: (event: ScrollEvent) => void
4574-
}
4575-
>
45764503
declare type ScrollViewProps = Readonly<
45774504
ViewProps & ScrollViewPropsIOS & ScrollViewPropsAndroid & ScrollViewBaseProps
45784505
>
@@ -6067,7 +5994,7 @@ export {
60675994
AlertOptions, // a0cdac0f
60685995
AlertType, // 5ab91217
60695996
AndroidKeyboardEvent, // e03becc8
6070-
Animated, // 0652b5d1
5997+
Animated, // 5254e138
60715998
AppConfig, // ce4209a7
60725999
AppRegistry, // 5edf0524
60736000
AppState, // 12012be5
@@ -6114,8 +6041,8 @@ export {
61146041
EventSubscription, // b8d084aa
61156042
ExtendedExceptionData, // 5a6ccf5a
61166043
FilterFunction, // bf24c0e3
6117-
FlatList, // 8c50f04a
6118-
FlatListProps, // e170f2c9
6044+
FlatList, // 869c5a5f
6045+
FlatListProps, // 2a81c428
61196046
FocusEvent, // 62fc1eb8
61206047
FontVariant, // 7c7558bb
61216048
GestureResponderEvent, // f693e9a5
@@ -6250,18 +6177,18 @@ export {
62506177
SafeAreaView, // 9589fa67
62516178
ScaledSize, // 07e417c7
62526179
ScrollEvent, // 5d529218
6253-
ScrollResponderType, // c6860ec8
6180+
ScrollResponderType, // 114c7cc8
62546181
ScrollToLocationParamsType, // d7ecdad1
6255-
ScrollView, // a3918d1a
6256-
ScrollViewImperativeMethods, // 7cd8d8de
6257-
ScrollViewProps, // 429fdd65
6182+
ScrollView, // 7180dc9b
6183+
ScrollViewImperativeMethods, // 3f95ab77
6184+
ScrollViewProps, // 57e1167c
62586185
ScrollViewPropsAndroid, // 44210553
62596186
ScrollViewPropsIOS, // b34b696c
62606187
ScrollViewScrollToOptions, // 3313411e
62616188
SectionBase, // b376bddc
6262-
SectionList, // 92031230
6189+
SectionList, // c7dfd290
62636190
SectionListData, // 119baf83
6264-
SectionListProps, // c0d0a46a
6191+
SectionListProps, // 54f880fa
62656192
SectionListRenderItem, // 1fad0435
62666193
SectionListRenderItemInfo, // 745e1992
62676194
Separators, // 6a45f7e3
@@ -6326,9 +6253,9 @@ export {
63266253
ViewStyle, // 00a0f8fb
63276254
VirtualViewMode, // 6be59722
63286255
VirtualizedList, // 68c7345e
6329-
VirtualizedListProps, // c7e8e7d7
6256+
VirtualizedListProps, // d414f5ca
63306257
VirtualizedSectionList, // 9fd9cd61
6331-
VirtualizedSectionListProps, // 53a7e6a4
6258+
VirtualizedSectionListProps, // 01460821
63326259
WrapperComponentProvider, // 9cf3844c
63336260
codegenNativeCommands, // 628a7c0a
63346261
codegenNativeComponent, // 2baac257

0 commit comments

Comments
 (0)