So the scenario is once I click a chevron button, an accordion component which placed right underneath of the chevron button opens and shows its children components.
However I don't want other chevron buttons share same state so that only a clicked chevron button with a related accordion expands.
And by default once the template component has just mounted, accordions expand showing its children with chevron buttons are up ^
codes:
// useChevron hook
import { useState } from 'react';
const useChevron = () => {
const [isChevronUp, setIsChevronUp] = useState<Partial<any>>({});
const handleChevron = (id: string) => {
setIsChevronUp((previousState) => ({
...previousState,
[id]: !previousState[id],
}));
};
return { isChevronUp, setIsChevronUp, handleChevron };
};
export default useChevron;
// template
...
const { handleChevron, isChevronUp } = useChevron();
...
<div id="containerOne">
// when it has just been rendered isChevronUp is empty object so in case set isOpen as true which makes chevron looks like ^ and shows accordion's children
<ChevronButton
isOpen={_.isEmpty(isChevronUp) ? true : !!isChevronUp.containerOne}
id='containerOne'
onClick={() => handleChevron('containerOne')}
/>
</div>
<Accordion isOpen={_.isEmpty(isChevronUp) ? true : !!isChevronUp.containerOne}>
<ChildrenComponent1 />
<ChildrenComponent2 />
...
It seems like working except I have to click twice the chevron button at very first time then toggling fine as expected.
I tried below:
const [isChevronUp, setIsChevronUp] = useState<Partial<SomeType>>({
'default': true
});
...
<ChevronButton
isOpen={Object.keys(isChevronUp).length === 1 ? isChevronUp.default : !!isChevronUp.containerOne}
id='containerOne'
onClick={() => handleChevron('containerOne')}
/>
</div>
<Accordion isOpen={Object.keys(isChevronUp).length === 1 ? isChevronUp.default : !!isChevronUp.containerOne}>
but it's the same having to click the chevron button twice.
What is the best way to set defualt chevron up and accordion expanded in this situation and solve the twice clicking stuff?
Thanks in advance.