|
2 | 2 | /* eslint-disable react-native/no-inline-styles */ |
3 | 3 |
|
4 | 4 | import React, { useEffect, useMemo, useRef, useState } from 'react'; |
5 | | -import { ScrollView, View } from 'react-native'; |
| 5 | +import { View } from 'react-native'; |
6 | 6 | import type { ResponsiveGridProps, TileItem } from './types'; |
7 | 7 | import { calcResponsiveGrid } from './calc-responsive-grid'; |
8 | 8 | import type { NativeSyntheticEvent, NativeScrollEvent } from 'react-native'; |
9 | 9 | import useThrottle from '../hooks/use-throttle'; |
| 10 | +import Animated, { useAnimatedScrollHandler } from 'react-native-reanimated'; |
10 | 11 | import { renderPropComponent } from '../libs/render-prop-component'; |
11 | 12 |
|
12 | 13 | export const ResponsiveGrid: React.FC<ResponsiveGridProps> = ({ |
@@ -84,45 +85,41 @@ export const ResponsiveGrid: React.FC<ResponsiveGridProps> = ({ |
84 | 85 | scrollEventInterval |
85 | 86 | ); |
86 | 87 |
|
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 | | - ); |
| 88 | + const throttledOnScroll = useThrottle((currentScrollY: number) => { |
| 89 | + scrollYPosition.current = currentScrollY; |
| 90 | + |
| 91 | + // Calculate the position to check against the threshold |
| 92 | + const contentHeight = gridViewHeight; |
| 93 | + const scrollViewHeight = containerSize.height; |
| 94 | + const threshold = onEndReachedThreshold * scrollViewHeight; |
| 95 | + |
| 96 | + // Check if we've reached the threshold for calling onEndReached |
| 97 | + if ( |
| 98 | + !onEndReachedCalled.current && |
| 99 | + currentScrollY + scrollViewHeight + threshold >= contentHeight |
| 100 | + ) { |
| 101 | + onEndReachedCalled.current = true; // Marked as called to prevent subsequent calls |
| 102 | + onEndReached?.(); // call the onEndReached function if it exists |
| 103 | + } |
| 104 | + |
| 105 | + // Reset the flag when scrolled away from the bottom |
| 106 | + if (currentScrollY + scrollViewHeight + threshold * 2 < contentHeight) { |
| 107 | + onEndReachedCalled.current = false; |
| 108 | + } |
118 | 109 |
|
119 | | - const onScroll = (event: NativeSyntheticEvent<NativeScrollEvent>) => { |
| 110 | + // Update visible items for virtualization |
| 111 | + if (virtualization) { |
| 112 | + throttledUpdateVisibleItems(); |
| 113 | + } |
| 114 | + }, 32); |
| 115 | + |
| 116 | + const onScroll = useAnimatedScrollHandler((event) => { |
120 | 117 | if (onScrollProp) { |
121 | 118 | onScrollProp(event); |
122 | 119 | } |
123 | 120 |
|
124 | | - throttledOnScroll(event); |
125 | | - }; |
| 121 | + throttledOnScroll(event.contentOffset.y); |
| 122 | + }); |
126 | 123 |
|
127 | 124 | useEffect(() => { |
128 | 125 | if (virtualization) { |
@@ -155,7 +152,7 @@ export const ResponsiveGrid: React.FC<ResponsiveGridProps> = ({ |
155 | 152 | setContainerSize({ width, height }); |
156 | 153 | }} |
157 | 154 | > |
158 | | - <ScrollView |
| 155 | + <Animated.ScrollView |
159 | 156 | onScroll={onScroll} |
160 | 157 | contentContainerStyle={{ |
161 | 158 | height: sumScrollViewHeight || '100%', |
@@ -195,7 +192,7 @@ export const ResponsiveGrid: React.FC<ResponsiveGridProps> = ({ |
195 | 192 | > |
196 | 193 | {renderPropComponent(FooterComponent)} |
197 | 194 | </View> |
198 | | - </ScrollView> |
| 195 | + </Animated.ScrollView> |
199 | 196 | </View> |
200 | 197 | ); |
201 | 198 | }; |
0 commit comments