Skip to content

Instantly share code, notes, and snippets.

@manuel-mauky
Created June 15, 2020 17:59
Show Gist options
  • Save manuel-mauky/703051fe64fa6593dba026de5ec186e4 to your computer and use it in GitHub Desktop.
Save manuel-mauky/703051fe64fa6593dba026de5ec186e4 to your computer and use it in GitHub Desktop.
React-Hooks-Article: Expansion Box implemented as Class
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>&#x25B2;</span> : <span>&#x25BC;</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