Skip to content

Instantly share code, notes, and snippets.

@jordiup
Created April 2, 2023 05:24
Show Gist options
  • Save jordiup/6eb4efb3201fb8ad923e7b43558d662c to your computer and use it in GitHub Desktop.
Save jordiup/6eb4efb3201fb8ad923e7b43558d662c to your computer and use it in GitHub Desktop.
Tabs Component in React Native, complete with Typescript Typings
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