callstack / callstack/react-native-pager-view
Wrapped pager for ease of use with separate components (without mapping function)
- Dominant language
- TypeScript
- Stars
- 3.4k
- Forks
- 476
- Avg merge
- 10d 21h
- Merged PRs (30d)
- 2
Description
## 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 (
);
}
```
Contributor guide
Assessment
This issue has not been assessed yet.