Created
June 15, 2020 18:01
-
-
Save manuel-mauky/5906bac8e93ddd43e0165695dea81d24 to your computer and use it in GitHub Desktop.
React-Hooks-Article: Expansion Box implemented as Function with useState Hook
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
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>▲</span> : <span>▼</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