Unique keys in React
Anyone know why I'm getting that warning here? I've a key for each
Would appreciate it if someone can spot the error I can't!
renderedItemrenderedItem returned, and for the nested ItemComponentItemComponent so am a bit confused. Haven't spent enough time banging my head against a React wall to spot it, and out of the 17 pages of errors React has provided me with none are helpful.Would appreciate it if someone can spot the error I can't!
export function FilterAccordion({ items, className }) {
const [expandedIndex, setExpandedIndex] = useState(0);
const handleClick = (index) => {
setExpandedIndex((currentExpandedIndex) => {
if (currentExpandedIndex === index) {
return -1;
}
return index;
});
};
const renderedItems = items.map((item, index) => {
const isExpanded = index === expandedIndex;
const ItemComponent = item.content.itemComponent;
const Icon = item.icon;
const classes = twMerge(
`group flex items-center px-3 py-2.5 rounded cursor-pointer bg-slate-100 hover:bg-teal-500 ${
isExpanded ? 'bg-teal-500 text-white' : ''
}`
);
const openIcon = (
<span className=''>
{isExpanded ? <GoChevronDown /> : <GoChevronLeft />}
</span>
);
return (
<div className='my-0.5' key={item.id}>
<div className={classes} onClick={() => handleClick(index)}>
<Icon className='mr-4 w-6 h-6 group-hover:text-white' />
<div className='text-base group-hover:text-white'>{item.name}</div>
<div className='ml-auto group-hover:text-white'>{openIcon}</div>
</div>
{isExpanded && (
<AccordionContent>
{item.content.options.map((option) => {
return <ItemComponent key={option} option={option} />;
})}
</AccordionContent>
)}
</div>
);
});
return <div className={className}>{renderedItems}</div>;
}export function FilterAccordion({ items, className }) {
const [expandedIndex, setExpandedIndex] = useState(0);
const handleClick = (index) => {
setExpandedIndex((currentExpandedIndex) => {
if (currentExpandedIndex === index) {
return -1;
}
return index;
});
};
const renderedItems = items.map((item, index) => {
const isExpanded = index === expandedIndex;
const ItemComponent = item.content.itemComponent;
const Icon = item.icon;
const classes = twMerge(
`group flex items-center px-3 py-2.5 rounded cursor-pointer bg-slate-100 hover:bg-teal-500 ${
isExpanded ? 'bg-teal-500 text-white' : ''
}`
);
const openIcon = (
<span className=''>
{isExpanded ? <GoChevronDown /> : <GoChevronLeft />}
</span>
);
return (
<div className='my-0.5' key={item.id}>
<div className={classes} onClick={() => handleClick(index)}>
<Icon className='mr-4 w-6 h-6 group-hover:text-white' />
<div className='text-base group-hover:text-white'>{item.name}</div>
<div className='ml-auto group-hover:text-white'>{openIcon}</div>
</div>
{isExpanded && (
<AccordionContent>
{item.content.options.map((option) => {
return <ItemComponent key={option} option={option} />;
})}
</AccordionContent>
)}
</div>
);
});
return <div className={className}>{renderedItems}</div>;
}