Skip to content

Add chunkSize to more efficiently display cards #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ yarn add react-native-reanimated react-native-gesture-handler
| swipeLeft | callback | Animates the card to fling to the left and calls onSwipeLeft |
| swipeTop | callback | Animates the card to fling to the top and calls onSwipeTop |

## Misc props

| props | type | description | default |
| :--------- | :------ | :------------------------------------------------------------ | :----------------------------------------- |
| chunkSize | number | The number of cards rendered at a given time | `3` |

## Usage 🧑‍💻

```typescript
Expand Down Expand Up @@ -369,6 +375,10 @@ type SwiperOptions<T> = {
OverlayLabelRight?: () => JSX.Element;
OverlayLabelLeft?: () => JSX.Element;
OverlayLabelTop?: () => JSX.Element;
/*
* Misc Props
*/
chunkSize?: number;
};
```

Expand Down
39 changes: 33 additions & 6 deletions src/Swiper.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useImperativeHandle, type ForwardedRef } from 'react';
import React, { useEffect, useImperativeHandle, useRef, type ForwardedRef } from 'react';

Check failure on line 1 in src/Swiper.tsx

View workflow job for this annotation

GitHub Actions / lint

Replace `·useEffect,·useImperativeHandle,·useRef,·type·ForwardedRef·` with `⏎··useEffect,⏎··useImperativeHandle,⏎··useRef,⏎··type·ForwardedRef,⏎`
import { runOnJS, useAnimatedReaction } from 'react-native-reanimated';
import { Dimensions } from 'react-native';
import type { SwiperCardRefType, SwiperOptions } from 'rn-swiper-list';
Expand Down Expand Up @@ -36,12 +36,16 @@
onSwipeStart,
onSwipeActive,
onSwipeEnd,
chunkSize = 3,
}: SwiperOptions<T>,
ref: ForwardedRef<SwiperCardRefType>
) => {
const { activeIndex, refs, swipeRight, swipeLeft, swipeBack, swipeTop } =
useSwipeControls(data);

const currentIndexRef = useRef(0);
const visibleCardsRef = useRef(data.slice(0, chunkSize));

useImperativeHandle(
ref,
() => {
Expand All @@ -55,6 +59,19 @@
[swipeLeft, swipeRight, swipeBack, swipeTop]
);

useEffect(() => {
visibleCardsRef.current = data.slice(0, chunkSize);
}, [data]);

Check failure on line 64 in src/Swiper.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has a missing dependency: 'chunkSize'. Either include it or remove the dependency array

const updateVisibleCards = () => {
const currentIndex = currentIndexRef.current;
if (currentIndex >= data.length - chunkSize) {
visibleCardsRef.current = data.slice(currentIndex, data.length);
} else {
visibleCardsRef.current = data.slice(currentIndex, currentIndex + chunkSize);

Check failure on line 71 in src/Swiper.tsx

View workflow job for this annotation

GitHub Actions / lint

Replace `currentIndex,·currentIndex·+·chunkSize` with `⏎········currentIndex,⏎········currentIndex·+·chunkSize⏎······`
}
};

useAnimatedReaction(
() => {
return activeIndex.value >= data.length;
Expand All @@ -67,12 +84,14 @@
[data]
);

return data.map((item, index) => {
return visibleCardsRef.current.map((item, index) => {
const actualIndex = currentIndexRef.current + index;

return (
<SwiperCard
key={index}
key={actualIndex}
cardStyle={cardStyle}
index={index}
index={actualIndex}
disableRightSwipe={disableRightSwipe}
disableLeftSwipe={disableLeftSwipe}
disableTopSwipe={disableTopSwipe}
Expand All @@ -92,12 +111,20 @@
OverlayLabelRight={OverlayLabelRight}
OverlayLabelLeft={OverlayLabelLeft}
OverlayLabelTop={OverlayLabelTop}
ref={refs[index]}
ref={refs[actualIndex]}
onSwipeRight={(cardIndex) => {
onSwipeRight?.(cardIndex);
if (currentIndexRef.current + chunkSize < data.length) {
currentIndexRef.current += 1;
updateVisibleCards();
}
}}
onSwipeLeft={(cardIndex) => {
onSwipeLeft?.(cardIndex);
if (currentIndexRef.current + chunkSize < data.length) {
currentIndexRef.current += 1;
updateVisibleCards();
}
}}
onSwipeTop={(cardIndex) => {
onSwipeTop?.(cardIndex);
Expand All @@ -106,7 +133,7 @@
onSwipeActive={onSwipeActive}
onSwipeEnd={onSwipeEnd}
>
{renderCard(item, index)}
{renderCard(item, actualIndex)}
</SwiperCard>
);
});
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export type SwiperOptions<T> = {
OverlayLabelRight?: () => JSX.Element;
OverlayLabelLeft?: () => JSX.Element;
OverlayLabelTop?: () => JSX.Element;
//* Misc
chunkSize?: number;
};
export type SwiperCardOptions = {
index: number;
Expand Down
Loading