Last active
May 24, 2020 20:12
-
-
Save nestarz/102c585aefe056078306d3e899c347e5 to your computer and use it in GitHub Desktop.
Tabs using React
This file contains hidden or 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, { useState } from "react"; | |
const classs = (object) => | |
Object.entries(object).reduce( | |
(str, [name, bool]) => `${str}${bool ? ` ${name}` : ""}`, | |
"" | |
); | |
const Tabs = ({ children }) => { | |
const [active, setActive] = useState(0); | |
return ( | |
<> | |
<nav> | |
{children.map(({ props: { name } }, index) => ( | |
<a | |
className={classs({ active: index === active })} | |
onClick={() => setActive(index)} | |
> | |
{name} | |
</a> | |
))} | |
</nav> | |
<main>{children[active]}</main> | |
</> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment