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)}
+ />
+
+ );
+}
+
+const styles = StyleSheet.create({
+ root: {
+ flex: 1,
+ padding: 8,
+ },
+ list: {
+ height: 200,
+ },
+ item: {
+ height: 40,
+ justifyContent: 'center',
+ paddingHorizontal: 8,
+ },
+});
+
+export default {
+ title: 'OnMomentumScroll',
+ name: 'onMomentumScroll',
+ description:
+ 'Counts `onMomentumScrollBegin`/`onMomentumScrollEnd` for animated and non-animated `scrollToOffset` commands.',
+ render: function () {
+ return ;
+ },
+} as RNTesterModuleExample;
diff --git a/packages/rn-tester/js/examples/FlatList/FlatListExampleIndex.js b/packages/rn-tester/js/examples/FlatList/FlatListExampleIndex.js
index 58071eeef0d5..1a83cf81b07a 100644
--- a/packages/rn-tester/js/examples/FlatList/FlatListExampleIndex.js
+++ b/packages/rn-tester/js/examples/FlatList/FlatListExampleIndex.js
@@ -17,6 +17,7 @@ import MaintainVisibleContentPosition from './FlatList-maintainVisibleContentPos
import MultiColumnExample from './FlatList-multiColumn';
import NestedExample from './FlatList-nested';
import OnEndReachedExample from './FlatList-onEndReached';
+import OnMomentumScrollExample from './FlatList-onMomentumScroll';
import OnStartReachedExample from './FlatList-onStartReached';
import onViewableItemsChangedExample from './FlatList-onViewableItemsChanged';
import onViewableItemsChanged_horizontal_noWaitForInteraction from './FlatList-onViewableItemsChanged-horizontal-noWaitForInteraction';
@@ -40,6 +41,7 @@ export default {
MaintainVisibleContentPosition,
OnStartReachedExample,
OnEndReachedExample,
+ OnMomentumScrollExample,
ContentInsetExample,
InvertedExample,
onViewableItemsChangedExample,
diff --git a/packages/rn-tester/js/examples/ScrollView/ScrollViewExample.js b/packages/rn-tester/js/examples/ScrollView/ScrollViewExample.js
index 5e68863e1764..b2f057e46dbf 100644
--- a/packages/rn-tester/js/examples/ScrollView/ScrollViewExample.js
+++ b/packages/rn-tester/js/examples/ScrollView/ScrollViewExample.js
@@ -421,6 +421,7 @@ const examples: Array = [
},
},
{
+ name: 'onMomentumScroll',
title: ' OnMomentumScroll\n',
description:
'An alert will be called when the momentum scroll starts or ends.',
@@ -1118,17 +1119,81 @@ const OnScrollOptions = () => {
};
const OnMomentumScroll = () => {
- const [scroll, setScroll] = useState('none');
+ const ref = useRef>(null);
+ const [beginCount, setBeginCount] = useState(0);
+ const [endCount, setEndCount] = useState(0);
+ const [lastEvent, setLastEvent] = useState('none');
+ // Counts a begin while momentum is already active, or an end without a
+ // matching begin.
+ const [errorCount, setErrorCount] = useState(0);
+ const isMomentumActiveRef = useRef(false);
+
+ const handleMomentumScrollBegin = useCallback(() => {
+ if (isMomentumActiveRef.current) {
+ setErrorCount(count => count + 1);
+ }
+ isMomentumActiveRef.current = true;
+ setBeginCount(count => count + 1);
+ setLastEvent('onMomentumScrollBegin');
+ }, []);
+
+ const handleMomentumScrollEnd = useCallback(() => {
+ if (!isMomentumActiveRef.current) {
+ setErrorCount(count => count + 1);
+ }
+ isMomentumActiveRef.current = false;
+ setEndCount(count => count + 1);
+ setLastEvent('onMomentumScrollEnd');
+ }, []);
+
+ // Leaves isMomentumActiveRef alone: it mirrors the native momentum state, so an
+ // end event still pending after a reset pairs with its begin.
+ const resetCounts = useCallback(() => {
+ setBeginCount(0);
+ setEndCount(0);
+ setErrorCount(0);
+ setLastEvent('none');
+ }, []);
+
return (
- Scroll State: {scroll}
+
+ Last event: {lastEvent}
+
+
+ begin:{beginCount}
+
+ end:{endCount}
+ errors:{errorCount}
setScroll('onMomentumScrollBegin')}
- onMomentumScrollEnd={() => setScroll('onMomentumScrollEnd')}
+ testID="momentum_scroll_view"
+ onMomentumScrollBegin={handleMomentumScrollBegin}
+ onMomentumScrollEnd={handleMomentumScrollEnd}
nestedScrollEnabled>
{ITEMS.map(createItemRow)}
+ ref.current?.scrollToEnd({animated: true})}
+ />
+ ref.current?.scrollTo({x: 0, y: 0, animated: true})}
+ />
+ ref.current?.scrollTo({x: 0, y: 0, animated: false})}
+ />
+
);
};