EventKey is a String type variable, your initial value is a number type variable. You can change the initial value of the active key to a string value and convert the index value to a string in the collapse.
import React, {useState} from 'react'
import {Accordion, Card , useAccordionToggle }from 'react-bootstrap'
import {
faPlus,
faMinus
} from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
function CustomToggle({ children, eventKey, handleClick }) {
const decoratedOnClick = useAccordionToggle(eventKey, () =>
handleClick()
);
return (
<button
type="button"
style={{ backgroundColor: 'transparent' }}
onClick={decoratedOnClick}
>
{children}
</button>
);
}
export default function Resources() {
const [activeKey, setActiveKey] = useState("0")
const data = [
{
title: 'Port-Harcourt',
content: 'Rough but very place '
},
{
title: 'Abuja',
content: 'Neat but a lot of diplomacy '
},
{
title: 'Lagos',
content: 'Neat and Rough, but a lot of politics'
},
]
return (
<Accordion defaultActiveKey={"0"} activeKey={activeKey}>
{
data.map((item, index) => (
<Card key={index}>
<Card.Header> {item.title}
<CustomToggle eventKey={index}
handleClick ={() => {
if (activeKey === index ) {
setActiveKey(null)
}else {
setActiveKey(index)
}
}}
>
{activeKey === index ? <FontAwesomeIcon icon={faMinus} /> : <FontAwesomeIcon icon={faPlus} />}
</CustomToggle>
</Card.Header>
<Accordion.Collapse eventKey={index.toString()}>
<Card.Body>
{item.content}
</Card.Body>
</Accordion.Collapse>
</Card>
))
}
</Accordion>
)
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…