MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Component Reusability and Polymorphic Components0%
Study Materials
React.js for IntermediateProgress 0%
1.1Component Reusability and Polymorphic Components1.2Render Props Pattern in Modern React1.3Higher-Order Components vs Modern Hooks1.4Compound Components Pattern Explained
HomeTutorialsReact.js for IntermediateComponent Reusability and Polymorphic Components
Render Props Pattern in Modern React

Component Reusability and Polymorphic Components

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

1. The Challenge of Flexible UI Primitives

In professional design systems (such as Radix UI, Headless UI, or custom enterprise component libraries), UI primitives like buttons, cards, and typography must adapt to diverse semantic roles without duplicating code.

Consider a <Button> component. In standard scenarios, it should render an HTML <button> element. However:

  • When linking to an external website, it semantically needs to render an <a> tag.
  • When used with React Router, it should render a client-side <Link> component.
  • In custom dashboard toolbars, it might render as a <div> with ARIA roles.

If you create separate <Button>, <LinkButton>, and <RouteButton> components, you fracture style maintenance, prop contracts, and accessibility attributes.

The solution is the Polymorphic Component Pattern (commonly implemented via the as prop).

1<Button> ──> Renders <button className="btn">
2<Button as="a" href="..."> ──> Renders <a href="..." className="btn">
3<Button as={Link} to="..."> ──> Renders <Link to="..." className="btn">

2. Implementing a Polymorphic Component in JavaScript/React

In React, JSX tag names that start with an uppercase letter are evaluated dynamically as variables. By accepting an as prop and aliasing it to a capitalized variable name (Component), we can dynamically determine the rendered tag:

1import React from 'react';
2 
3export default function Text({
4 as: Component = 'p', // Default to paragraph if omitted
5 variant = 'body', // 'heading' | 'subheading' | 'body' | 'caption'
6 children,
7 className = '',
8 ...restProps
9}) {
10 const variantClasses = {
11 heading: 'text-3xl font-extrabold text-slate-900 tracking-tight',
12 subheading: 'text-xl font-bold text-slate-800',
13 body: 'text-base text-slate-600 leading-relaxed',
14 caption: 'text-xs text-slate-400 uppercase tracking-wider'
15 };
16 
17 return (
18 <Component
19 className={`${variantClasses[variant] || variantClasses.body} ${className}`}
20 {...restProps}
21 >
22 {children}
23 </Component>
24 );
25}

Flexible Consumer Usage:

1// Renders <h1> with heading styles
2<Text as="h1" variant="heading">MSK Course Directory</Text>
3 
4// Renders <span> with caption styles
5<Text as="span" variant="caption">Module 4 of 12</Text>
6 
7// Renders default <p> with body styles
8<Text>Master modern full-stack development patterns.</Text>

3. Building Polymorphic Action Buttons

Here is an enterprise polymorphic button supporting native buttons, external anchors, and custom framework routers:

1import React from 'react';
2 
3export default function ActionButton({
4 as: Element = 'button',
5 variant = 'primary',
6 children,
7 ...props
8}) {
9 const baseStyles = 'inline-flex items-center justify-center px-4 py-2 font-medium rounded-lg transition';
10 const variants = {
11 primary: 'bg-blue-600 text-white hover:bg-blue-700',
12 secondary: 'bg-slate-100 text-slate-800 hover:bg-slate-200',
13 danger: 'bg-red-600 text-white hover:bg-red-700'
14 };
15 
16 // If rendering a native button without explicit type, default to 'button'
17 const defaultType = Element === 'button' ? { type: 'button' } : {};
18 
19 return (
20 <Element
21 className={`${baseStyles} ${variants[variant]}`}
22 {...defaultType}
23 {...props}
24 >
25 {children}
26 </Element>
27 );
28}

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What is a "polymorphic component" in React?
Question 2
Why is the prop destructured as as: Component = 'div'?
Question 3
What semantic accessibility risk is avoided by using polymorphic components?
Question 4
If <ActionButton as="a" href="https://mskinstitute.com">Enroll</ActionButton> is rendered, what HTML tag will appear in the DOM?
Question 5
What happens if the as prop is omitted when using <Text>Hello</Text> with as: Component = 'p'?
Next Lesson

Render Props Pattern in Modern React

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
NoneRender Props Pattern in Modern React

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master React.js for Intermediate with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
Next
PreviousPrevNext