Created
April 2, 2023 05:24
-
-
Save jordiup/6eb4efb3201fb8ad923e7b43558d662c to your computer and use it in GitHub Desktop.
Tabs Component in React Native, complete with Typescript Typings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { ReactNode, useEffect, useState } from 'react'; | |
import { View } from 'react-native'; | |
interface TabsProps { | |
tabIndex: number; | |
children: ReactNode[]; | |
} | |
export const Tabs: React.FC<TabsProps> = ({ tabIndex, children }) => { | |
const [activeTab, setActiveTab] = useState(0); | |
useEffect(() => { | |
setActiveTab(tabIndex); | |
}, [tabIndex]); | |
return ( | |
<View> | |
{React.Children.map(children, (child, index) => | |
index === activeTab ? child : null, | |
)} | |
</View> | |
); | |
}; | |
interface TabProps { | |
children: ReactNode; | |
} | |
export const Tab: React.FC<TabProps> = ({ children }) => { | |
return <View>{children}</View>; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment