I was playing with React Hooks and I tried to create an accordion. I am trying to itreate it from an array. So I have 2data, When I consoled the index, I'm getting the exact index of the clicked button. But when I consoled the ref , I'm getting the ref of the second one but not the first one.
How is this happening? Is it a bug in react?
Here is the sample code
const content = useRef(null);
const accordionClick = (id) => {
setHeightState(setActive === false ? "0px" : `${content.current.scrollHeight}px`);
SetCustomIndex(id);
console.log(id);
console.log(content );
};
items.general.map((data, index) => (
<div className={`accordion-container`} key={index}>
<button className{`accordion ${setActive}`} onClick{()=>accordionClick(index)}>
{data}
</button>
<div
className={`panel`}
ref={content}
data-id={index}
style={{
height: `${setHeight}`,
}}
data-id={index}
>
<p className="description-14">{data.item.main}</p>
</div>
idin the state. The way you're trying do solve your task now is not the declarative React way.