Last active
December 26, 2020 08:23
-
-
Save kwoncharles/f0b113e01932c07624045cee77c7ba58 to your computer and use it in GitHub Desktop.
Render Props Tab
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
// Tabs.jsx | |
import React from 'react' | |
function Tabs({ children }) { | |
const [selectedTab, setTab] = React.useState(initialTab) | |
return ( | |
<ul className="tab-container"> | |
{children(selectedTab, setTab)} | |
</ul> | |
) | |
} | |
Tabs.Item = ({ isSelected, onSelect, children }) => ( | |
<li | |
onClick={onSelect} | |
className={`tab-item ${isSelected ? 'selected' : ''}`} | |
> | |
{children} | |
</li> | |
) |
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
// Page.jsx | |
function Page() { | |
return ( | |
<Tabs> | |
{(selectedTab, setTab) => ( | |
tabItems.map(tabItem => ( | |
<Tabs.Item | |
isSelected={tabItem === selectedTab} | |
onSelect={() => setTab(tabItem)} | |
> | |
{tabItem} | |
</Tabs.Item> | |
)) | |
)} | |
</Tabs> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment