Skip to content

Instantly share code, notes, and snippets.

@kwoncharles
Last active December 26, 2020 08:23
Show Gist options
  • Save kwoncharles/f0b113e01932c07624045cee77c7ba58 to your computer and use it in GitHub Desktop.
Save kwoncharles/f0b113e01932c07624045cee77c7ba58 to your computer and use it in GitHub Desktop.
Render Props Tab
// 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>
)
// 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