[BUG] - Accordion: dynamic items and start content is descendant of a button
Describe the bug
I've got some annoying errors/warnings to report when using the Accordion component:
- Dynamic items: throws a react warning that
Warning: The final argument passed to useMemo changed size between renders. The order and size of this array must remain constant. - The second issue when using a component with buttons in text or the startContent of the item, the following error shows :
Warning: validateDOMNesting(...): <button> cannot appear as a descendant of <button>. - another warning always shows:
Warning: Unknown event handler propertyonFocusChange. It will be ignored
Your Example Website or App
No response
Steps to Reproduce the Bug or Issue
Jus using the components with dynamic items and custom components on the trigger.
<Accordion
variant='splitted'
itemClasses={itemClasses}
className=' flex w-full flex-col gap-1 p-2 '
selectionMode='multiple'
{categories?.map((category, index) => (
<AccordionItem
key={'c_' + index}
aria-label={category?.name}
title={title_comp(category)}
>
{category?.products?.map((product, idx) => (
<Product key={'p_' + idx} product={product} />
))}
{...}
</AccordionItem>
))}
</Accordion>
Expected behavior
Everything should work without any errors or warnings.
Screenshots or Videos
Errors:
Operating System Version
Windows 11
Browser
Edge
The final argument passed to useMemo changed size
I believe this actually might be a larger issue with the general approach taken to variant props + useMemo. Anywhere the ...Object.values(variantProps) pattern is used can be affected by this issue whenever the variant props in use are dynamically changed.
There are quite a few spots -> https://github.com/search?q=repo%3Anextui-org%2Fnextui+...Object.values%28variantProps%29&type=code
I am not sure if there are perf implications or some other reason this code is in place, but if https://github.com/nextui-org/nextui/blob/62d307117a4ef5464834289901fc8c7a87f59025/packages/core/system-rsc/src/utils.ts#L55-L62 was adjusted along the lines below, the number of arguments to useMemo would never change and would resolve this issue.
const picked = variantKeys.reduce((acc, key) => {
return { ...acc, [key]: props[key] };
}, {});
any update on this?
@7prog @vamshi-fission I couldn't reproduce the issue just based on the given snippet. Please provide a minimal reproducible example on stackblitz.
@7prog @vamshi-fission I couldn't reproduce the issue just based on the given snippet. Please provide a minimal reproducible example on stackblitz. `
<AccordionItem key="1" aria-label="Travelers" startContent={ Travelers:
<p
className="overflow-x-auto whitespace-nowrap px-1 text-lg"
ref={tabsRef}
>
{updatedTravelersGroup
?.filter((traveler) => traveler.count > 0)
?.map((traveler) =>
traveler.count === 1 && traveler.name.endsWith("s")
? `${traveler.count} ${traveler.name.slice(0, -1)}`
: `${traveler.count} ${traveler.name}`
)
.join(", ")}
</p>
<Button
onClick={handleEditTravelers}
color="primary"
isIconOnly
aria-label="Like"
size="sm"
className=" p-1 rounded-full"
>
<MdModeEdit
size={8}
className=" h-6 w-6 text-white m-[0.2rem]"
/>
</Button>
</section>
}
>
{/* Content inside accordion */}
{updatedTravelersGroup
?.filter((interest) => interest.count > 0)
?.map((traveler) => (
<section
key={traveler.id}
className="text-lg mb-[1rem] xl:lg:flex"
>
<span className="font-[500] mr-1">{`Interests for the`}</span>
<span className="me-2 font-[500]">
{traveler.count === 1 && traveler.name.endsWith("s")
? traveler.name.slice(0, -1)
: traveler.name}
:
</span>
{interestsState?.interests
.filter(
(interest) =>
interest.selected &&
interest.traveler_group_id === traveler.id
)
.map((interest, index) => (
<span key={interest.id}>
{index > 0 ? ", " : ""}
{interest.name.replace(/\([^)]*\)/, "")}
</span>
))}
<span
onClick={() => handleEditInterests(traveler)}
className="bg-blue-600 flex rounded-full items-center justify-center cursor-pointer h-6 w-6 xl:lg:ml-2"
>
<MdModeEdit size={16} className="text-white m-1" />
</span>
</section>
))}
</AccordionItem>
</Accordion>`
I am using Button inside startCotent on AccordianItem, and it throws the error in console.
could only reproduce Warning: validateDOMNesting(...): <button> cannot appear as a descendant of <button>.. Please provide a sandbox instead.
@7prog for the error Warning: validateDOMNesting(...):
<AccordionItem is a button, and inside your component title={title_comp(category)} has another button i presume.
In my case this solve my problem, on the button i use as='a'
<Button as='a' isIconOnly size="sm" variant="light">
...
</Button>
now 2025/3/29
I recently encountered the same problem,
An exception occurred when using the tabs component: <button> cannot appear as a descendant of <button>
reason:
used .map in multiple tabs and they had the same key so an exception occurred. This should be a bug
env: "@heroui/alert": "^2.2.16", "@heroui/button": "^2.2.16", "@heroui/card": "^2.2.15", "@heroui/link": "^2.2.13", "@heroui/navbar": "^2.2.14", "@heroui/pagination": "^2.2.14", "@heroui/popover": "^2.3.16", "@heroui/switch": "^2.2.14", "@heroui/system": "^2.4.12", "@heroui/tabs": "^2.2.13", "@heroui/theme": "^2.4.12", "@heroui/toast": "^2.0.6", "react": "^18.0.0", "react-dom": "^18.0.0", "react-icons": "^5.5.0", "react-redux": "^9.2.0", "react-router-dom": "^6.29.0",
Incomplete code :
<Tabs aria-label="Options" className={"flex-row-reverse"}
variant={"light"} radius={"none"} fullWidth={false}
defaultSelectedKey={"grid"}>
<Tab key="grid"
title={<MdViewModule className={"size-7 "}/>} className={" p-1"}>
<BooksCard list={list}/>
</Tab>
<Tab key="list"
title={<MdViewList className={"size-7 "}/>} className={" p-1"}>
<BooksList list={list}/>
</Tab>
</Tabs>
BooksCard:
{
list.map((item, index) =>
<div className={" px-3 pb-2 pt-3 "} key={index}>
<Card
className=" h-[16rem] w-[12rem] border-2 shadow-none hover:shadow-md hover:transition-all hover:duration-250"
radius={"sm"}>
<img alt="" className={" object-center "} src={HSOT + "api/book/cover/"+item["k"]}/>
<CardHeader className={"absolute bottom-[2.5rem] text-white "}>
<p title={item["attr"].find((item)=> item["k"] === "name")["v"]}
className="text-tiny overflow-hidden text-wrap">{item["attr"].find((item)=> item["k"] === "name")["v"]}</p>
</CardHeader>
<CardFooter className=" absolute bottom-0 bg-white/50 justify-around inline-flex px-1 py-2 m-0 ">
<Popover color={"primary"} placement="top" backdrop={"transparent"}>
<PopoverTrigger>
<Button className="text-tiny text-white " color={"primary"} radius="sm"
size="sm">info</Button>
</PopoverTrigger>
<PopoverContent>
<div
className={"text-medium max-w-[18rem] max-h-svh min-w-44 "}>
{
item["attr"].map((item2,index2)=>
<p className="row-span-1 text-medium overflow-hidden text-wrap "
key={index+"-" + index2}>
<span className={"font-bold"}>{item2['k']}:</span>
{item2['v']}
</p>
)
}
</div>
</PopoverContent>
</Popover>
<Button className="text-tiny text-white " color={"success"} radius="sm"
size="sm">down</Button>
</CardFooter>
</Card>
</div>
)}
BooksList :
{
list.map((item, index) =>
<div
className={"small:col-span-6 middle:col-span-6 normal:col-span-3 large:col-span-2 h-fit "}
key={index}>
<Card
className={"w-full h-32 border-1 border-sky-300 shadow-none inline-flex flex-row "}
radius={"sm"} isPressable={true} disableRipple={true}>
<img alt="" className={"w-32 object-cover "}
src={HSOT + "api/book/cover/" + item["k"]}/>
<CardBody className={"w-full grid grid-flow-row place-items-stretch bg-white "}>
{
item["attr"].map((item2, index2) =>
<p className="row-span-1 text-small overflow-auto text-wrap truncate "
key={index + index2}>
<span className={"font-bold"}>{item2['k']}:</span>
<span>{item2['v']}</span>
</p>
)
}
</CardBody>
<CardFooter className={"max-w-28 bg-sky-200 "}>
<div className={"h-full flex flex-col justify-around mx-auto gap-2 "}>
<div></div>
<Button size={"sm"} color={"primary"} radius={"sm"}>down</Button>
</div>
</CardFooter>
</Card>
</div>
)}
Correct code:
Avoid exception by modifying the keys of different tab labels. eg: BooksList :
{
list.map((item, index) =>
<div
className={"small:col-span-6 middle:col-span-6 normal:col-span-3 large:col-span-2 h-fit "}
key={"list-"+index}>
<Card
className={"w-full h-32 border-1 border-sky-300 shadow-none inline-flex flex-row "}
radius={"sm"} isPressable={true} disableRipple={true}>
<img alt="" className={"w-32 object-cover "}
src={HSOT + "api/book/cover/" + item["k"]}/>
<CardBody className={"w-full grid grid-flow-row place-items-stretch bg-white "}>
{
item["attr"].map((item2, index2) =>
<p className="row-span-1 text-small overflow-auto text-wrap truncate "
key={"list-" +index + "-" + index2}>
<span className={"font-bold"}>{item2['k']}:</span>
<span>{item2['v']}</span>
</p>
)
}
</CardBody>
<CardFooter className={"max-w-28 bg-sky-200 "}>
<div className={"h-full flex flex-col justify-around mx-auto gap-2 "}>
<div></div>
<Button size={"sm"} color={"primary"} radius={"sm"}>下载</Button>
</div>
</CardFooter>
</Card>
</div>
)}
This is a problem caused by the tab tag. Should be corrected
any update on this issue or potential fixes in the upcoming versions?
Closing in favor of v3. If the issue is reproducible in v3, please create a new one with a minimal reproducible example.