useAccordion

The useAccordion hook is used inside the Accordion component. This hook handles the collapse logic, inside the Accordion component.

Code


import { useState } from "react"

export default function useAccordion(){
    const [openAccordion, setOpenAccordion] = useState(null);

    function toggle(id) {
        if (openAccordion == id) {
            return setOpenAccordion(null);
        }

        setOpenAccordion(id);
    }

    return [openAccordion, toggle];
}

Explanation

  • As you can see above it uses the useState hook of the React internally for toggling the state of each accordion label.

  • It doesn't accept any parameters.

  • It returns the following:

    • openAccordion - the current state for the label.

    • toggle - a function to toggle the label state.

Last updated