r/reactjs • u/DependerSethi • May 20 '26
Resource Polymorphic components are the Swiss Army knife of React design systems - as prop vs asChild explained
Kept running into the same problem at work: build a Button component, someone needs it as a link, now you're either duplicating styles or writing invalid html by nesting <button> inside <a>
Wrote up how polymorphic components solve this. covers:
- the as prop pattern (how chakra and mantine do it)
- making it type-safe with ComponentPropsWithoutRef<C> so typescript knows which props are valid per element
- the forwardRef gotcha where typescript loses the generic (and the fix)
- asChild pattern (how radix/shadcn does it) and why it exists
- a reusable PolymorphicProps utility type you can drop into any project
also when NOT to use any of this. not every component needs to be polymorphic.
https://www.sethi.io/blog/polymorphic-components-one-component-many-forms
Curious how others handle this in their design systems. do you go with as or asChild?
16
u/sorinmocuta May 20 '26
Yeah, it’s more related to library maintainers, but tbh, congrats on the article. Really nice deep dive.
12
u/Super-Otter May 20 '26
Funny it says
asChildis better for TypeScript performance, when usingasChildmeans you have no Type safety, since you can't enforce that the child that user renders are actually compatible with the props you're adding to it.Never really like this
asChildpattern because of this. Personally I think any API that loses type-safety is a no-go for libraries.A
renderprop is probably the simplest approach to implement such features, doesn't have complexities ofasprop or type unsafety ofasChild. Alternatively a hook that returns props that you spread on your component is also a valid pattern if it's just behavior and you don't actually need to render anything yourself.2
u/BlazingThunder30 May 20 '26
BaseUI does this using render={}. Which is almost the same as asChild. I think it's a nice way to separate styling from semantics. For a button and such components it integrates well with routing too, since I often need either <button>, <a> with href, or <Link> from routing library, depending on the circumstances. It's nice that I'm able to use the same button in all of those cases.
11
u/Merry-Lane May 20 '26 edited May 20 '26
It’s over-engineering and I think there are way better options.
For instance, you could make it way more simple by having two/three/… different components (reusing the same style variables and types/apis for instance) and just let the dev use whichever makes most sense.
If you really want a single component, you can make a wrapper that returns one or the other depending on the value of a prop. You can make types strictly correct depending on the value of this prop if they have to differ.
But no, honestly, it’s totally the kind of scenario where I would just make them use the same const styles.
3
u/dfltr May 20 '26
This looks like a ChatGPT rewrite of a blog post by Steve Kinney: https://stevekinney.com/courses/react-typescript/polymorphic-components-and-as-prop
2
u/BloodThirstyWool May 20 '26
I was pretty confused switching to shadcn with Base UI and having to change to using the render prop instead - based on your comments is it a good idea to export a ButtonLink component in the button.tsx file?
9
u/olssoneerz May 20 '26
I do design systems. Huge fan of polymorphic for "foundational"/atom-level and typography components.
2
u/Raziel_LOK May 20 '26 edited May 20 '26
Only useful for niche cases, design system and library work. If you are the people providing components or creating design systems. Yeah, this is useful. For consumers this is usually recipe for convoluted components that people don't understand. There are a bunch of ways to do the same without needing that. 1-tailwind classes as strings that can be put in a link or a button. 2-classnames that can be used the same way as 1.
I personally tend to avoid it unless I am using it to provide the component exported as a button and as link. You don't want consumers to use that.
EDIT: bunch of typos
1
u/Dethstroke54 May 21 '26
Missing the render props solution is disappointing bc it’s one of the simplest and most effective. It’s also one of the most widely used now. Unlike the imo hack that is asChild renderProp render props and as are both much closer to clean React patterns. Render props is basically how react handles slots as well.
Prop collisions or merging are generally avoided with good practices. The user has full control over the props that are being forwarded in this pattern, which imo further helps encourage best practice and eliminates the opaque merging regardless.
It’s clean. I’d encourage you check out how Mantine has it setup.
2
u/DatTommel May 20 '26
React19 makes forwardref easy: https://react.dev/blog/2024/12/05/react-19#ref-as-a-prop
1
u/United_Reaction35 May 21 '26
Javascript is to polymorphism as the army mess-hall is to cuisine.
1
u/nerdly90 May 21 '26
As a react noob, this was a super helpful read. Thanks!
1
u/Vis_et_Honor May 21 '26
There are other approaches, but feel like Radix really popularized `asChild` and similar. Conceptually, `as` might actually be better due to the TypeSafety it offers. Also the new Go TypeScript compiler might actually make the performance cost a non-factor.
-8
u/saito200 May 20 '26
i generally let Claude code handle the implementation details and i look at the output and ask for changes. also i use vue, i avoid react because vue is simpler
1
u/[deleted] May 22 '26
[removed] — view removed comment