Skip to content

Commit 5162816

Browse files
janpemeta-codesync[bot]
authored andcommitted
fix: fix getNativeScrollRef return type for FlatList (#54735)
Summary: ### The Problem When trying to measure the location of a View within a FlatList (ie. for scrolling to the view), the current recommended method is to use measureLayout on the nested view to determine its location inside the containing FlatList: ``` const MyComponent = () => { const flatListRef = useRef<FlatList>(null); const nestedViewRef = useRef<View>(null); const scrollToNestedView = () => { if (!flatListRef.current || !nestedViewRef.current) { return; } nestedViewRef.current.measureLayout( flatListRef.current.getNativeScrollRef(), (x, y) => { flatListRef.current.scrollTo({ y, animated: true }); }, ); } return ( <FlatList ref={flatListRef}> <View ref={nestedViewRef}> { /* content */ } </View> </FlatList> ); } ``` However the types for `FlatList` `getNativeScrollRef` don't allow this. ### The solution This solution is basically identical to that in #52203. The return value for `getNativeScrollRef` should be `HostInstance | null` ## Changelog:[GENERAL] [FIXED] - Change FlatList.getNativeScrollRef return type definition to allow accessing the underlying HostInstance. Pull Request resolved: #54735 Test Plan: None needed. This is only a type update exposing existing functionality. Reviewed By: zeyap Differential Revision: D104393366 Pulled By: huntie fbshipit-source-id: 700f708d9a39b16af3e0ed90a748051361101627
1 parent e2e6553 commit 5162816

2 files changed

Lines changed: 58 additions & 43 deletions

File tree

packages/react-native/Libraries/Components/ScrollView/ScrollView.d.ts

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ interface ScrollResponderMixin extends SubscribableMixin {
264264
* down to make it meet the keyboard's top. Default is false.
265265
*/
266266
scrollResponderScrollNativeHandleToKeyboard(
267-
nodeHandle: any,
267+
nodeHandle: number | HostInstance,
268268
additionalOffset?: number,
269269
preventNegativeScrollOffset?: boolean,
270270
): void;
@@ -836,10 +836,29 @@ export interface ScrollViewProps
836836
StickyHeaderComponent?: React.ComponentType<any> | undefined;
837837
}
838838

839-
declare class ScrollViewComponent extends React.Component<ScrollViewProps> {}
840-
export declare const ScrollViewBase: Constructor<ScrollResponderMixin> &
841-
typeof ScrollViewComponent;
842-
export class ScrollView extends ScrollViewBase {
839+
export interface ScrollViewScrollToOptions {
840+
x?: number | undefined;
841+
y?: number | undefined;
842+
animated?: boolean | undefined;
843+
}
844+
845+
// Public methods for ScrollView
846+
export interface ScrollViewImperativeMethods {
847+
/**
848+
* Returns a reference to the underlying scroll responder, which supports
849+
* operations like `scrollTo`. All ScrollView-like components should
850+
* implement this method so that they can be composed while providing access
851+
* to the underlying scroll responder's methods.
852+
*/
853+
readonly getScrollResponder: () => ScrollResponderType;
854+
readonly getScrollableNode: () => number | undefined;
855+
readonly getInnerViewNode: () => number | undefined;
856+
readonly getInnerViewRef: () => React.ComponentRef<typeof View> | null;
857+
/**
858+
* Returns a reference to the underlying native scroll view, or null if the
859+
* native instance is not mounted.
860+
*/
861+
readonly getNativeScrollRef: () => HostInstance | null;
843862
/**
844863
* Scrolls to a given x, y offset, either immediately or with a smooth animation.
845864
* Syntax:
@@ -850,18 +869,11 @@ export class ScrollView extends ScrollViewBase {
850869
* the function also accepts separate arguments as an alternative to the options object.
851870
* This is deprecated due to ambiguity (y before x), and SHOULD NOT BE USED.
852871
*/
853-
scrollTo(
854-
y?:
855-
| number
856-
| {
857-
x?: number | undefined;
858-
y?: number | undefined;
859-
animated?: boolean | undefined;
860-
},
872+
readonly scrollTo: (
873+
options?: ScrollViewScrollToOptions | number,
861874
deprecatedX?: number,
862875
deprecatedAnimated?: boolean,
863-
): void;
864-
876+
) => void;
865877
/**
866878
* A helper function that scrolls to the end of the scrollview;
867879
* If this is a vertical ScrollView, it scrolls to the bottom.
@@ -870,32 +882,39 @@ export class ScrollView extends ScrollViewBase {
870882
* The options object has an animated prop, that enables the scrolling animation or not.
871883
* The animated prop defaults to true
872884
*/
873-
scrollToEnd(options?: {animated?: boolean | undefined}): void;
874-
885+
readonly scrollToEnd: (options?: ScrollViewScrollToOptions | null) => void;
875886
/**
876887
* Displays the scroll indicators momentarily.
877888
*/
878-
flashScrollIndicators(): void;
879-
880-
/**
881-
* Returns a reference to the underlying scroll responder, which supports
882-
* operations like `scrollTo`. All ScrollView-like components should
883-
* implement this method so that they can be composed while providing access
884-
* to the underlying scroll responder's methods.
885-
*/
886-
getScrollResponder(): ScrollResponderMixin;
887-
888-
getScrollableNode(): any;
889+
readonly flashScrollIndicators: () => void;
890+
scrollResponderZoomTo(
891+
rect: {
892+
x: number;
893+
y: number;
894+
width: number;
895+
height: number;
896+
animated?: boolean | undefined;
897+
},
898+
animated?: boolean, // deprecated, put this inside the rect argument instead
899+
): void;
900+
scrollResponderScrollNativeHandleToKeyboard(
901+
nodeHandle: number | HostInstance,
902+
additionalOffset?: number,
903+
preventNegativeScrollOffset?: boolean,
904+
): void;
905+
}
889906

890-
// Undocumented
891-
getInnerViewNode(): any;
907+
export type ScrollResponderType = ScrollViewImperativeMethods;
892908

893-
/**
894-
* Returns a reference to the underlying native scroll view, or null if the
895-
* native instance is not mounted.
896-
*/
897-
getNativeScrollRef: () => HostInstance | null;
909+
export interface PublicScrollViewInstance
910+
extends HostInstance,
911+
ScrollViewImperativeMethods {}
898912

913+
declare class ScrollViewComponent extends React.Component<ScrollViewProps> {}
914+
export declare const ScrollViewBase: Constructor<ScrollResponderMixin> &
915+
typeof ScrollViewComponent;
916+
export interface ScrollView extends ScrollViewImperativeMethods {}
917+
export class ScrollView extends ScrollViewBase {
899918
/**
900919
* @deprecated Use scrollTo instead
901920
*/

packages/react-native/Libraries/Lists/FlatList.d.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ import type {
1414
VirtualizedListProps,
1515
ViewabilityConfig,
1616
} from '@react-native/virtualized-lists';
17-
import type {ScrollViewComponent} from '../Components/ScrollView/ScrollView';
17+
import type {PublicScrollViewInstance} from '../Components/ScrollView/ScrollView';
1818
import type {StyleProp} from '../StyleSheet/StyleSheet';
1919
import type {ViewStyle} from '../StyleSheet/StyleSheetTypes';
20-
import type {View} from '../Components/View/View';
2120

2221
export interface FlatListProps<ItemT> extends VirtualizedListProps<ItemT> {
2322
/**
@@ -229,13 +228,10 @@ export abstract class FlatListComponent<
229228
getScrollResponder: () => React.JSX.Element | null | undefined;
230229

231230
/**
232-
* Provides a reference to the underlying host component
231+
* Returns a reference to the underlying native scroll view, or null if the
232+
* native instance is not mounted.
233233
*/
234-
getNativeScrollRef: () =>
235-
| React.ComponentRef<typeof View>
236-
| React.ComponentRef<typeof ScrollViewComponent>
237-
| null
238-
| undefined;
234+
getNativeScrollRef: () => PublicScrollViewInstance | null;
239235

240236
getScrollableNode: () => any;
241237

0 commit comments

Comments
 (0)