Skip to content

Commit 280c135

Browse files
fix: improve performance on smaller devices
1 parent 6c970da commit 280c135

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/Date/Swiper.native.tsx

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,16 @@ const styles = StyleSheet.create({
2626
position: 'relative',
2727
},
2828
})
29-
const visibleArray = (i: number) => [i - 2, i - 1, i, i + 1, i + 2]
29+
30+
function getVisibleArray(
31+
i: number,
32+
{ isHorizontal, height }: { isHorizontal: boolean; height: number }
33+
) {
34+
if (isHorizontal || height < 700) {
35+
return [i - 1, i, i + 1]
36+
}
37+
return [i - 2, i - 1, i, i + 1, i + 2]
38+
}
3039

3140
function Swiper(props: SwiperProps) {
3241
return (
@@ -49,17 +58,17 @@ function SwiperInner({
4958
height,
5059
}: SwiperProps & { width: number; height: number }) {
5160
const idx = React.useRef<number>(initialIndex)
61+
const isHorizontal = scrollMode === 'horizontal'
5262
const [visibleIndexes, setVisibleIndexes] = React.useState<number[]>(
53-
visibleArray(initialIndex)
63+
getVisibleArray(initialIndex, { isHorizontal, height })
5464
)
55-
const isHorizontal = scrollMode === 'horizontal'
5665

5766
const parentRef = React.useRef<ScrollView | null>(null)
5867

5968
const scrollTo = React.useCallback(
6069
(index: number, animated: boolean) => {
6170
idx.current = index
62-
setVisibleIndexes(visibleArray(index))
71+
setVisibleIndexes(getVisibleArray(index, { isHorizontal, height }))
6372

6473
if (!parentRef.current) {
6574
return
@@ -82,7 +91,7 @@ function SwiperInner({
8291
})
8392
}
8493
},
85-
[parentRef, isHorizontal, width]
94+
[parentRef, isHorizontal, width, height]
8695
)
8796

8897
const onPrev = React.useCallback(() => {
@@ -111,10 +120,10 @@ function SwiperInner({
111120

112121
if (idx.current !== newIndex) {
113122
idx.current = newIndex
114-
setVisibleIndexes(visibleArray(newIndex))
123+
setVisibleIndexes(getVisibleArray(newIndex, { isHorizontal, height }))
115124
}
116125
},
117-
[idx, isHorizontal]
126+
[idx, height, isHorizontal]
118127
)
119128

120129
const renderProps = {
@@ -162,7 +171,7 @@ function SwiperInner({
162171
]}
163172
>
164173
{visibleIndexes
165-
? [0, 1, 2, 3, 4].map((vi) => (
174+
? new Array(visibleIndexes.length).fill(undefined).map((_, vi) => (
166175
<View
167176
key={vi}
168177
style={{

0 commit comments

Comments
 (0)