Reusable Components in React
Learn to create reusable React components using props, consistent UI patterns and practical examples from CIIT Training Institute.
01 Learning Objectives
After completing this lesson, you will be able to:
Write a component once and use it multiple times.
Customize component content through props.
Keep your application easier to maintain.
Create course cards, student cards and buttons.
02 What Are Reusable Components?
A reusable component is a component that can be rendered in different places with different data. Instead of writing separate markup for every course or student, we create one component and customize it using props.
function CourseCard({ title, duration }) {
return (
<div>
<h3>{title}</h3>
<p>Duration: {duration}</p>
</div>
);
}
export default CourseCard;
The same CourseCard component can display React JS,
.NET Full Stack and Python course details.
React JS
Duration: 3 Months
.NET Full Stack
Duration: 6 Months
Python
Duration: 4 Months
03 Reusable Component Diagram
The parent component sends different props to the same reusable component. Each instance renders its own content.
import CourseCard from "./CourseCard";
function App() {
return (
<div>
<h2>CIIT Training Courses</h2>
<CourseCard
title="React JS"
duration="3 Months"
/>
<CourseCard
title=".NET Full Stack"
duration="6 Months"
/>
<CourseCard
title="Python"
duration="4 Months"
/>
</div>
);
}
export default App;
CIIT Training Courses
React JS
Duration: 3 Months
.NET Full Stack
Duration: 6 Months
Python
Duration: 4 Months
04 Reusable Student Card
We can reuse one student component to display multiple students with different names and courses.
function StudentCard({ name, course }) {
return (
<div>
<h3>{name}</h3>
<p>Course: {course}</p>
</div>
);
}
export default StudentCard;
import StudentCard from "./StudentCard";
function App() {
return (
<div>
<h2>CIIT Student Profiles</h2>
<StudentCard
name="Samadhan"
course="React JS"
/>
<StudentCard
name="Samiksha"
course=".NET Full Stack"
/>
<StudentCard
name="Pradnya"
course="Python"
/>
</div>
);
}
export default App;
CIIT Student Profiles
05 Creating a Reusable Button
A reusable button component can accept its label and button type through props. Here we use a click handler passed from the parent component.
function ActionButton({ label, onClick }) {
return (
<button type="button" onClick={onClick}>
{label}
</button>
);
}
export default ActionButton;
import ActionButton from "./ActionButton";
function App() {
const showMessage = () => {
alert("Welcome to CIIT Training Institute!");
};
return (
<div>
<ActionButton
label="Start Learning"
onClick={showMessage}
/>
<ActionButton
label="Contact Institute"
onClick={showMessage}
/>
</div>
);
}
export default App;
These are visual examples. In the running React app, clicking either button displays the configured alert.
06 Benefits of Reusable Components
Less Repeated Code
Write the structure once and reuse it.
Consistency
Keep the same UI structure across the application.
Easy Maintenance
Update a shared component in one place.
Flexible UI
Customize component instances using props.
07 Important Rules and Best Practices
Use props to customize reusable components.
Keep components focused on one clear responsibility.
Pass event handlers as props when components need to respond to user actions.
Avoid duplicating the same markup in multiple places.
Use descriptive names such as StudentCard and CourseCard.
08 Practice Questions
- Create a reusable
CourseCardcomponent with title and duration props. - Display three students using one
StudentCardcomponent. - Create a reusable button that accepts a label prop.
- Create a reusable profile card that accepts name and role props.
- Explain how reusable components reduce repeated code.
- Build a course listing page using reusable components.
Summary
Reusable Components allow developers to define a UI structure once and render it in multiple places with different props. They reduce duplicated code, improve consistency and simplify maintenance. At CIIT Training Institute, practise reusability by creating course cards, student profiles and buttons that can be customized with different data and event handlers.