const AccordionItem = ({ title, opened, toggle, children }) => (
<div>
<button onClick={toggle}>{title}</button>
{opened && children}
</div>
);
const Accordion = ({ items }) => {
const [ opened, setOpened ] = useState(null);
return (
<div>
{items.map((n, i) => (
<AccordionItem
title={n.title}
opened={i === opened}
toggle={setOpened.bind(null, i === opened ? null : i)}
>
<p>{n.text}</p>
</AccordionItem>
))}
</div>
);
};
const [openId, setOpenId] = useState();
return (<>{elems.map(el => <AccElement key={el.id} open={opendId===el.id} onClick={() => setOpenId(el.id)} data={el} />}</>);