Created
June 15, 2020 17:59
-
-
Save manuel-mauky/703051fe64fa6593dba026de5ec186e4 to your computer and use it in GitHub Desktop.
React-Hooks-Article: Expansion Box implemented as Class
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 from "react" | |
import "./expansion-box.css" | |
type Props = { | |
title: string | |
} | |
type State = { | |
expanded: boolean | |
} | |
export class ExpansionBoxClass extends React.Component<Props, State> { | |
constructor(props: Props) { | |
super(props) | |
this.state = { | |
expanded: false, | |
} | |
} | |
private switchExpansion() { | |
this.setState((prevState) => ({ expanded: !prevState.expanded })) | |
} | |
render() { | |
return ( | |
<div className={`expansion-box ${this.state.expanded ? "expanded" : ""}`}> | |
<header> | |
<h1>{this.props.title}</h1> | |
<button onClick={() => this.switchExpansion()}> | |
{this.state.expanded ? <span>▲</span> : <span>▼</span>} | |
</button> | |
</header> | |
{this.state.expanded && <section>{this.props.children}</section>} | |
</div> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment