import React, { useCallback, useEffect, useMemo, useRef, useState, } from 'react'; import { Pressable, ScrollView, StyleSheet, Text, View } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; import PagerView, { type PagerViewRef } from 'react-native-pager-view'; import { Button } from './component/Button'; import { colors, radius } from './theme'; import { BGCOLOR, padNumber } from './utils'; const PAGE_COUNT = 20; const PRELOAD_DISTANCES = [0, 1, 2, 3]; type LazyPageProps = { index: number; onMountChange: (index: number, mounted: boolean) => void; }; function LazyPage({ index, onMountChange }: LazyPageProps) { useEffect(() => { onMountChange(index, true); return () => onMountChange(index, false); }, [index, onMountChange]); return ( {`Mounted page ${index + 1}`} {Array.from({ length: 30 }, (_, row) => ( {`Page ${index + 1} row ${ row + 1 }`} ))} ); } type SegmentedProps = { label: string; swatchColor: string; testID: string; options: readonly number[]; value: number; onChange: (value: number) => void; }; function Segmented({ label, swatchColor, testID, options, value, onChange, }: SegmentedProps) { return ( {label} {options.map((option) => { const isActive = option === value; return ( onChange(option)} style={[styles.segment, isActive && styles.segmentActive]} > {option} ); })} ); } export function LazyPagerViewExample() { const ref = useRef(null); const [page, setPage] = useState(0); const [preloadDistance, setPreloadDistance] = useState(1); const [mounted, setMounted] = useState>(new Set()); const onMountChange = useCallback( (index: number, isMounted: boolean) => setMounted((prev) => { const next = new Set(prev); if (isMounted) { next.add(index); } else { next.delete(index); } return next; }), [] ); const pages = useMemo( () => Array.from({ length: PAGE_COUNT }, (_, index) => ( )), [onMountChange] ); const isFirst = page === 0; const isLast = page === PAGE_COUNT - 1; return ( ( {`Loading page ${ index + 1 }`} )} onPageSelected={(e) => setPage(e.nativeEvent.position)} > {pages}