Skip to content

Instantly share code, notes, and snippets.

@manuel-mauky
Created June 15, 2020 18:01
Show Gist options
  • Save manuel-mauky/5906bac8e93ddd43e0165695dea81d24 to your computer and use it in GitHub Desktop.
Save manuel-mauky/5906bac8e93ddd43e0165695dea81d24 to your computer and use it in GitHub Desktop.
React-Hooks-Article: Expansion Box implemented as Function with useState Hook
import React, { FC, useState } from "react"
import "./expansion-box.css"
type Props = {
title: string
}
export const ExpansionBoxFunction: FC<Props> = (props) => {
const [expanded, setExpanded] = useState(false)
const switchExpansion = () => {
setExpanded(!expanded)
}
return (
<div className={`expansion-box ${expanded ? "expanded" : ""}`}>
<header>
<h1>{props.title}</h1>
<button onClick={switchExpansion}>{expanded ? <span>&#x25B2;</span> : <span>&#x25BC;</span>}</button>
</header>
{expanded && <section>{props.children}</section>}
</div>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment