@@ -21,6 +21,7 @@ export const ResponsiveGrid: React.FC<ResponsiveGridProps> = ({
2121 style = { } ,
2222 itemContainerStyle = { } ,
2323 itemUnitHeight,
24+ onScroll : onScrollProp ,
2425 onEndReached,
2526 onEndReachedThreshold = 0.5 , // default to 50% of the container height
2627 keyExtractor = ( _ , index ) => String ( index ) , // default to item index if no keyExtractor is provided
@@ -83,33 +84,44 @@ export const ResponsiveGrid: React.FC<ResponsiveGridProps> = ({
8384 scrollEventInterval
8485 ) ;
8586
86- const onScroll = ( event : NativeSyntheticEvent < NativeScrollEvent > ) => {
87- const currentScrollY = event . nativeEvent . contentOffset . y ;
88- scrollYPosition . current = currentScrollY ;
89-
90- // Calculate the position to check against the threshold
91- const contentHeight = gridViewHeight ;
92- const scrollViewHeight = containerSize . height ;
93- const threshold = onEndReachedThreshold * scrollViewHeight ;
94-
95- // Check if we've reached the threshold for calling onEndReached
96- if (
97- ! onEndReachedCalled . current &&
98- currentScrollY + scrollViewHeight + threshold >= contentHeight
99- ) {
100- onEndReachedCalled . current = true ; // Marked as called to prevent subsequent calls
101- onEndReached ?.( ) ; // call the onEndReached function if it exists
102- }
87+ const throttledOnScroll = useThrottle (
88+ ( event : NativeSyntheticEvent < NativeScrollEvent > ) => {
89+ const currentScrollY = event . nativeEvent . contentOffset . y ;
90+ scrollYPosition . current = currentScrollY ;
91+
92+ // Calculate the position to check against the threshold
93+ const contentHeight = gridViewHeight ;
94+ const scrollViewHeight = containerSize . height ;
95+ const threshold = onEndReachedThreshold * scrollViewHeight ;
96+
97+ // Check if we've reached the threshold for calling onEndReached
98+ if (
99+ ! onEndReachedCalled . current &&
100+ currentScrollY + scrollViewHeight + threshold >= contentHeight
101+ ) {
102+ onEndReachedCalled . current = true ; // Marked as called to prevent subsequent calls
103+ onEndReached ?.( ) ; // call the onEndReached function if it exists
104+ }
105+
106+ // Reset the flag when scrolled away from the bottom
107+ if ( currentScrollY + scrollViewHeight + threshold * 2 < contentHeight ) {
108+ onEndReachedCalled . current = false ;
109+ }
110+
111+ // Update visible items for virtualization
112+ if ( virtualization ) {
113+ throttledUpdateVisibleItems ( ) ;
114+ }
115+ } ,
116+ 32
117+ ) ;
103118
104- // Reset the flag when scrolled away from the bottom
105- if ( currentScrollY + scrollViewHeight + threshold * 2 < contentHeight ) {
106- onEndReachedCalled . current = false ;
119+ const onScroll = ( event : NativeSyntheticEvent < NativeScrollEvent > ) => {
120+ if ( onScrollProp ) {
121+ onScrollProp ( event ) ;
107122 }
108123
109- // Update visible items for virtualization
110- if ( virtualization ) {
111- throttledUpdateVisibleItems ( ) ;
112- }
124+ throttledOnScroll ( event ) ;
113125 } ;
114126
115127 useEffect ( ( ) => {
@@ -145,7 +157,6 @@ export const ResponsiveGrid: React.FC<ResponsiveGridProps> = ({
145157 >
146158 < ScrollView
147159 onScroll = { onScroll }
148- scrollEventThrottle = { 32 }
149160 contentContainerStyle = { {
150161 height : sumScrollViewHeight || '100%' ,
151162 width : containerSize . width ,
0 commit comments