From cd718348682c7d33da5f7b98df5a1c53388daac5 Mon Sep 17 00:00:00 2001 From: Peter Abbondanzo Date: Wed, 11 Nov 2026 12:38:58 -0800 Subject: [PATCH] Add momentum scroll event counters to RNTester examples (#59940) Summary: Make the momentum scroll RNTester examples testable end to end. `` OnMomentumScroll now shows the last event, begin and end counts, and an error count that increments when `onMomentumScrollBegin` arrives while momentum is already active or `onMomentumScrollEnd` arrives without a matching begin. It adds buttons for animated `scrollToEnd`, animated and non-animated `scrollTo`, and resetting the counts, and is addressable as `onMomentumScroll`. A new `` OnMomentumScroll example counts begin and end events for animated and non-animated `scrollToOffset`. Changelog: [Internal] Differential Revision: D131232441 --- .../FlatList/FlatList-onMomentumScroll.js | 110 ++++++++++++++++++ .../examples/FlatList/FlatListExampleIndex.js | 2 + .../examples/ScrollView/ScrollViewExample.js | 73 +++++++++++- 3 files changed, 181 insertions(+), 4 deletions(-) create mode 100644 packages/rn-tester/js/examples/FlatList/FlatList-onMomentumScroll.js diff --git a/packages/rn-tester/js/examples/FlatList/FlatList-onMomentumScroll.js b/packages/rn-tester/js/examples/FlatList/FlatList-onMomentumScroll.js new file mode 100644 index 000000000000..7fb91a1de14f --- /dev/null +++ b/packages/rn-tester/js/examples/FlatList/FlatList-onMomentumScroll.js @@ -0,0 +1,110 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow strict-local + * @format + */ + +'use strict'; + +import type {RNTesterModuleExample} from '../../types/RNTesterTypes'; + +import * as React from 'react'; +import {useCallback, useRef, useState} from 'react'; +import {Button, FlatList, StyleSheet, Text, View} from 'react-native'; + +const DATA = [...Array(30)].map((_, i) => `Item ${i}`); + +component FlatList_onMomentumScroll() { + const ref = useRef>(null); + const [beginCount, setBeginCount] = useState(0); + const [endCount, setEndCount] = useState(0); + + const renderItem = useCallback( + ({item}: {item: string, ...}) => ( + + {item} + + ), + [], + ); + + const resetCounts = useCallback(() => { + setBeginCount(0); + setEndCount(0); + }, []); + + return ( + + begin:{beginCount} + end:{endCount} + index.toString()} + renderItem={renderItem} + getItemLayout={(_data: unknown, index: number) => ({ + length: 40, + offset: 40 * index, + index, + })} + style={styles.list} + testID="flatlist_momentum_list" + onMomentumScrollBegin={() => setBeginCount(count => count + 1)} + onMomentumScrollEnd={() => setEndCount(count => count + 1)} + /> +