Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

94
Views
Toggle accordion items

I have a React Js accordion, when i click an item, the panel opens, to close it you have to click on another item, i need to add the possibility of closing the active panel after clicking the AccButton

Here is my code :

const { active, setActive } = useContext(AccordionContext)

const eventHandler = (e, index) => {
    e.preventDefault();
    setActive(index);
}

return (
    <AccItem id={props.index}>
        <AccButton
            onClick={(e) => eventHandler(e, props.index)}
            aria-expanded={ active === props.index ? 'true' : 'false' }
            aria-disabled={ active === props.index ? 'true' : 'false' }
        >
            <AccItemTitle>
                {props.description}
            </AccItemTitle>
        </AccButton>

        <AccPanel elWidth={props.elWidth}>
            <PanelToggle toggle={active === props.index}></PanelToggle>
        </AccPanel>

    </AccItem>
)
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could try to add a check if active !== index and otherwise set active to null. Like this:

const eventHandler = (e, index) => {
    e.preventDefault();
    setActive(index !== active ? index : null);
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!