Skip to content

Commit 4d6b757

Browse files
committed
refactor: refactor autoplay logic
1 parent e43e4b6 commit 4d6b757

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/Carousel.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,19 +124,19 @@ function Carousel<T>(
124124
}, [onScrollBegin]);
125125

126126
const scrollViewGestureOnScrollBegin = React.useCallback(() => {
127-
!autoPlay && pause();
127+
pause();
128128
_onScrollBegin();
129-
}, [_onScrollBegin, pause, autoPlay]);
129+
}, [_onScrollBegin, pause]);
130130

131131
const _onScrollEnd = React.useCallback(() => {
132132
computedIndex();
133133
onScrollEnd?.(sharedPreIndex.current, sharedIndex.current);
134134
}, [sharedPreIndex, sharedIndex, computedIndex, onScrollEnd]);
135135

136136
const scrollViewGestureOnScrollEnd = React.useCallback(() => {
137-
autoPlay && run();
137+
run();
138138
_onScrollEnd();
139-
}, [_onScrollEnd, run, autoPlay]);
139+
}, [_onScrollEnd, run]);
140140

141141
const offsetX = useDerivedValue(() => {
142142
const totalSize = size * data.length;

src/hooks/useAutoPlay.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,23 @@ export function useAutoPlay(opts: {
2424
if (timer.current) {
2525
pause();
2626
}
27+
28+
if (!autoPlay) {
29+
return;
30+
}
31+
2732
timer.current = setInterval(() => {
2833
autoPlayReverse
2934
? carouselController.prev()
3035
: carouselController.next();
3136
}, autoPlayInterval);
32-
}, [autoPlayReverse, carouselController, autoPlayInterval, pause]);
37+
}, [
38+
pause,
39+
autoPlay,
40+
autoPlayReverse,
41+
autoPlayInterval,
42+
carouselController,
43+
]);
3344

3445
React.useEffect(() => {
3546
if (autoPlay) {

0 commit comments

Comments
 (0)