callstack / callstack/react-native-pager-view

Wrapped pager for ease of use with separate components (without mapping function)

Offen
#912 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
TypeScript
Sterne
3.4k
Forks
476
Ø Merge
10 T. 21 Std.
Gemergte PRs (30 T.)
2

Beschreibung

## Describe the feature
This wraps the pagerview into a separate component and looks for the children within the pagerview and based on that will memoize the components.

## Motivation
I struggled to find a way of using the usePagerView hook without a mapping function. For example when you want to show multiple components. So I thought of this idea and I just wanted to share this with everyone :)

Let me know what you think about this! Maybe someone else has an even better alternative? Happy coding!

## Related Issues

Issue right now (look at comment):
```typescript
export function PagerHookExample() {
const { AnimatedPagerView, ref, ...rest } = usePagerView({ pagesAmount: 10 });

return (


{useMemo(
() =>
rest.pages.map((_, index) => ( // We're mapping here, but what if we want to show multiple components?



{`page number ${index}`}


)),
[rest.pages]
)}



);
}
```

Step one: Create the component
```typescript
import { forwardRef, Children, useMemo, useEffect } from "react";
import PagerView, { UsePagerViewProps } from "react-native-pager-view";

interface ListProps extends Omit {
children: React.ReactNode;
onListCountChange?: (count: number) => void;
}

const Pager = forwardRef(
({ children, onListCountChange, AnimatedPagerView, scrollEnabled, ...other }, ref) => {
const childCount = Children.count(children);

useEffect(() => {
onListCountChange?.(childCount);
}, [childCount]);

return (

{useMemo(
() =>
Children.map(children, (child, index) => {
return child
}),
[children],
)}

);
},
);

export default Pager;
```

Step two: use the component using the usePagerView hook
```typescript
export default function Screen() {
const { activePage, ref, ...pagerViewProps } = usePagerView();
return (





);
}

```

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Beginne mit dem usePagerView-Hook und dem AnimatedPagerView-Einstiegspunkt, die im Issue beschrieben sind, und vergleiche anschließend die PagerHookExample- und Screen-Beispiele. Die Arbeit ist abgeschlossen, wenn eine separate Pager-Komponente mehrere untergeordnete Komponenten ohne eine Mapping-Funktion akzeptieren kann und dabei die hier gezeigten Pager-Props und das Navigationsverhalten beibehält.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
react-native, typescript
Bereich
frontend, mobile
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
32/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.