Skip to content

Instantly share code, notes, and snippets.

@nestarz
Last active May 24, 2020 20:12
Show Gist options
  • Save nestarz/102c585aefe056078306d3e899c347e5 to your computer and use it in GitHub Desktop.
Save nestarz/102c585aefe056078306d3e899c347e5 to your computer and use it in GitHub Desktop.
Tabs using React
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