Skip to content

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.

Updated 3 December 2026Tested with
  • 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

  1. Step 1: More than one child inside asChild

    Slot requires 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>
  2. Step 2: Wrapping the child in a Fragment (<>...</>)

    A React Fragment cannot receive className, ref or click handlers. Replace the fragment with a real DOM element (<a>, <button>, <span>) or a component that forwards props.

  3. Step 3: Custom component does not spread props

    If the child is your own wrapper component (e.g. <AppLink>), it must forward ...props (including className, onClick and ref in 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} />;}
  4. Step 4: Using asChild in a Base UI shadcn/ui project

    Base UI components do not have an asChild prop — they use render instead (<Button render={<a href="/pricing/" />}>See pricing</Button>). Check components/ui/button.tsx to 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).

Was this page helpful?

Still stuck? We answer every message ourselves. Write to us

Building with shadcn/ui and Tailwind CSS v4?

Install 188 free blocks and components with the shadcn CLI (no account), preview them in your own brand in Studio, or get all 1,570 blocks and every theme in UI Pro (€199 once).

Use it with

This page is built from UI Pro parts: the docs article block (docs-quernly) with the code, terminal, install command, steps, contents and feedback components, included in UI Pro.