import React, { useRef, useEffect } from 'react'; import { View } from 'react-native'; import Swiper, { SwiperInstance } from '../Swiper'; import type { TabsProps } from './type'; export type TabsContentProps = Pick & { onChange?: (index: number) => void; currentIndex: number; }; const TabsContent: React.FC = props => { const { children, animated, swipeable } = props; const innerEffect = useRef(false); const swiperRef = useRef(null); const renderChildren = () => { if (animated || swipeable) { return ( { if (innerEffect.current) { innerEffect.current = false; return; } if (props.onChange) props.onChange(idx); }} > {React.Children.map(children, child => ( {child} ))} ); } return children; }; const swipeToCurrentTab = (index: number) => { const swipe = swiperRef.current; if (!swipe) return; if (swipe.activeIndex !== index) { innerEffect.current = true; swipe.swipeTo(index); } }; useEffect(() => { swipeToCurrentTab(props.currentIndex); }, [props.currentIndex]); return {renderChildren()}; }; export default TabsContent;