shadcn/ui Button asChild not working: Slot errors and fixes
Fix React.Children.only and missing styles with shadcn/ui Button asChild: single child rule, fragments, forwarding props, and Base UI render prop.
- shadcn 4.23.0
- Tailwind CSS 4.3.5
- Vite 8
- React 19.3
Passing asChild to a shadcn/ui Button, DialogTrigger or TooltipTrigger tells Radix Slot to merge its classes and event handlers onto your child element instead of rendering a wrapper <button>. When it fails, you either get the crash React.Children.only expected to receive a single React element child or your styles/clicks disappear.
Four causes and fixes
Step 1: More than one child inside asChild
Slotrequires exactly one React element child. Placing an icon next to a<Link>, or a conditional expression alongside an element, crashes at runtime:wrong-vs-right.tsxTSX // Wrong: two direct children inside Button asChild<Button asChild> <ArrowRight className="size-4" /> <a href="/pricing/">See pricing</a></Button> // Right: put the icon inside the single child element<Button asChild> <a href="/pricing/"> <ArrowRight className="size-4" /> See pricing </a></Button>Step 2: Wrapping the child in a Fragment (<>...</>)
A React Fragment cannot receive
className,refor click handlers. Replace the fragment with a real DOM element (<a>,<button>,<span>) or a component that forwards props.Step 3: Custom component does not spread props
If the child is your own wrapper component (e.g.
<AppLink>), it must forward...props(includingclassName,onClickandrefin React 19) to the underlying<a>element, otherwise Radix cannot attach the trigger handlers or button styles:app-link.tsxTSX export function AppLink({ className, ...props }: React.ComponentProps<"a">) { return <a className={className} {...props} />;}Step 4: Using asChild in a Base UI shadcn/ui project
Base UI components do not have an
asChildprop — they userenderinstead (<Button render={<a href="/pricing/" />}>See pricing</Button>). Checkcomponents/ui/button.tsxto see whether your project uses Radix or Base UI, or read Base UI vs Radix.
Questions
When should I use Button asChild instead of buttonVariants()?
Both produce identical HTML (a single <a> styled like a button). Use <Button asChild><a href="..."> when you want the same component API everywhere, or <a className={buttonVariants({ variant: "outline" })}> when you want zero Slot overhead in a plain link.
Why does my Link inside DialogTrigger asChild not open the dialog?
Check that your custom link component spreads ...props onto the DOM element and does not overwrite onClick without calling props.onClick?.(e).
Related articles
- shadcn/uishadcn/ui Base UI vs Radix: which primitive library to pickCompare Radix UI (asChild, data-state) and Base UI (render prop, data-open) in shadcn/ui: composition, Tailwind v4 animations, registries, and when to pick each.
- shadcn/uishadcn/ui Combobox not scrolling inside a DialogFix a shadcn/ui Combobox or Popover inside a Dialog: set modal={true} on Popover, stop wheel propagation on CommandList, and fix focus trapping.
- shadcn/uishadcn/ui not working with Tailwind v4: six causes and fixesUnstyled or transparent shadcn components on Tailwind v4 come from v3 leftovers: the old PostCSS plugin, @tailwind directives, hsl() wrappers, missing imports.
Still stuck? We answer every message ourselves. Write to us