polymorphic component
A component that can render as different HTML elements or components, for example a Button that renders as a link.
In more detail
The older approach is an as prop, such as <Button as="a">, which makes TypeScript types large and slow and can break ref forwarding. The asChild pattern achieves the same goal by letting the consumer pass the element to render.
Example
function Box({ as: Tag = "div", children }) { return <Tag>{children}</Tag>; }Related terms
- asChild — A prop that tells a component to render its single child element instead of its own, merging its classes, props and behaviour onto that child.
- slot — A place in a component's layout that the consumer fills with their own content, either through a named prop such as icon or through a Slot component.
- ref — A React handle that points at a real DOM element, so code can focus it, measure it or attach listeners.
- semantic HTML — Using HTML elements for their meaning, such as <button> for actions and <nav> for navigation, rather than generic <div>s.
- prop — An input passed to a React component, written like an HTML attribute, such as size="lg" in <Button size="lg">.
- TypeScript — JavaScript with added type annotations, checked by a compiler before the code runs.
Taught in
- Polymorphism & Slotted Composition: The asChild Pattern — Design Systems & Component Engineering
- Component Purity: Pure Functions, Idempotence And Side Effects — React 19 & Modern Component Architecture
- Props Contract: Immutability, Children, And Polymorphic Tags — React 19 & Modern Component Architecture
- useImperativeHandle: Controlled Ref Forwarding Boundaries — React 19 & Modern Component Architecture
- Lifting State Up: Colocation, Ownership, And Data Flow — React 19 & Modern Component Architecture
- The use() Hook: Reading Promises And Context Directly — React 19 & Modern Component Architecture