I would like to make an accordion with react but do that when I click on a title it’s all the others that are displayed instead of just one.
I would like to make an accordion with react but do that when I click on a title it’s all the others that are displayed instead of just one.
import { useState } from "react"
import "./sidebar.css"
import sublinks from '../../data'
const Sidebar = () => {
const [showInfo, setShowInfo] = useState(false)
console.log(showInfo)
return (
<div className="sidebar show">
<div className="sidebar__container">
<button className="close-btn">x</button>
<div className="sidebar__container__w">
{sublinks.map((item, index) =>{
const {links, page}= item
return (
<div className="sidebar__menu__wrapper" key={index}>
<button
className="sidebar__menu"
onClick={() => setShowInfo(!showInfo)}
>
{page}
</button>
{showInfo && (
<div className=" side__Links" key={index} >
{links.map((link,index) =>{
const {label,url} = link
return (
<a key={index} href={url} className="side__Link">
{label}
</a>
)
})}
</div>
) }
</div>
)
})
}
</div>
</div>
</div>
)
}
export default Sidebar
2
Answers
You have a boolean state variable which though affects all links.
So what you need to have at your state is an array state variable which stores the opened accordions,
for the button click
and finally for the show/hide boolean part
That is because you only have "one" condition and all the sections use it.
You can use a js object instead of a single boolean.
Try this:
Or if you want to open one section at a time, do this: