Skip to content

Commit 6045796

Browse files
authored
Merge pull request #80 from dohooo/fix/issue_79
refactor: refactor useAutoPlay hooks
2 parents d158497 + 56c5f84 commit 6045796

File tree

4 files changed

+35
-21
lines changed

4 files changed

+35
-21
lines changed

example/src/components/SBImageItem.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ import React from 'react';
22
import { StyleSheet, View, Image, ActivityIndicator } from 'react-native';
33

44
const ISBImageItem: React.FC = () => {
5+
const uri = React.useRef(
6+
`https://picsum.photos/400/300?t=${new Date().getTime()}`
7+
);
58
return (
69
<View style={styles.container}>
710
<ActivityIndicator size="small" />
811
<Image
912
style={styles.image}
1013
source={{
11-
uri: `https://picsum.photos/800/600?t=${new Date().getTime()}`,
14+
uri: uri.current,
1215
}}
1316
/>
1417
</View>

example/src/normal/index.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const PAGE_WIDTH = window.width;
1111
function Index() {
1212
const [isVertical, setIsVertical] = React.useState(false);
1313
const [isFast, setIsFast] = React.useState(false);
14+
const [isAutoPlay, setIsAutoPlay] = React.useState(false);
1415

1516
const baseOptions = isVertical
1617
? ({
@@ -29,7 +30,7 @@ function Index() {
2930
<Carousel
3031
{...baseOptions}
3132
loop
32-
autoPlay
33+
autoPlay={isAutoPlay}
3334
autoPlayInterval={isFast ? 100 : 1500}
3435
data={[...new Array(6).keys()]}
3536
renderItem={() => <SBImageItem />}
@@ -48,6 +49,13 @@ function Index() {
4849
>
4950
{isFast ? 'NORMAL' : 'FAST'}
5051
</SButton>
52+
<SButton
53+
onPress={() => {
54+
setIsAutoPlay(!isAutoPlay);
55+
}}
56+
>
57+
AutoPlay:{`${isAutoPlay}`}
58+
</SButton>
5159
</View>
5260
);
5361
}

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-
pause();
127+
!autoPlay && pause();
128128
_onScrollBegin();
129-
}, [_onScrollBegin, pause]);
129+
}, [_onScrollBegin, pause, autoPlay]);
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-
run();
137+
autoPlay && run();
138138
_onScrollEnd();
139-
}, [_onScrollEnd, run]);
139+
}, [_onScrollEnd, run, autoPlay]);
140140

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

src/hooks/useAutoPlay.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,29 @@ export function useAutoPlay(opts: {
1616

1717
const timer = React.useRef<NodeJS.Timer>();
1818

19-
const run = React.useCallback(() => {
20-
if (autoPlay) {
21-
timer.current = setInterval(() => {
22-
autoPlayReverse
23-
? carouselController.prev()
24-
: carouselController.next();
25-
}, autoPlayInterval);
26-
}
27-
}, [autoPlay, autoPlayReverse, carouselController, autoPlayInterval]);
28-
2919
const pause = React.useCallback(() => {
3020
timer.current && clearInterval(timer.current);
3121
}, []);
3222

23+
const run = React.useCallback(() => {
24+
if (timer.current) {
25+
pause();
26+
}
27+
timer.current = setInterval(() => {
28+
autoPlayReverse
29+
? carouselController.prev()
30+
: carouselController.next();
31+
}, autoPlayInterval);
32+
}, [autoPlayReverse, carouselController, autoPlayInterval, pause]);
33+
3334
React.useEffect(() => {
34-
run();
35-
return () => {
36-
!!timer.current && clearInterval(timer.current);
37-
};
38-
}, [run, timer]);
35+
if (autoPlay) {
36+
run();
37+
} else {
38+
pause();
39+
}
40+
return pause;
41+
}, [run, pause, autoPlay]);
3942

4043
return {
4144
run,

0 commit comments

Comments
 (0)