Created
September 23, 2020 21:26
-
-
Save lai32290/7cae1336f2023cfe7d8a5ac3bcc0e30f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// accordion.js | |
import React from 'react' | |
import AccordionContents from './accordion-contents' | |
class Accordion extends React.Component { | |
state = {openIndex: 0} | |
setOpenIndex = openIndex => this.setState({openIndex}) | |
render() { | |
const {openIndex} = this.state | |
return ( | |
<div> | |
{this.props.items.map((item, index) => ( | |
<> | |
<button onClick={() => this.setOpenIndex(index)}> | |
{item.title} | |
</button> | |
{index === openIndex ? ( | |
<AccordionContents>{item.contents}</AccordionContents> | |
) : null} | |
</> | |
))} | |
</div> | |
) | |
} | |
} | |
export default Accordion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment