Component Lifecycle in React
Understand how React components are created, updated and removed from the user interface at CIIT Training Institute.
01 Learning Objectives
After completing this lesson, you will be able to:
Learn the stages of a React component.
Learn when a component appears on the screen.
Learn how state changes update the UI.
Learn when a component is removed.
02 What Is Component Lifecycle?
The React Component Lifecycle describes the stages a component passes through during its existence. A component can be mounted when it appears, updated when its data changes, and unmounted when it is removed from the UI.
useEffect() Hook to perform side effects and manage
cleanup when necessary.
import { useEffect } from "react";
function Welcome() {
useEffect(() => {
console.log("Welcome component mounted");
return () => {
console.log("Welcome component unmounted");
};
}, []);
return <h2>Welcome to CIIT Training Institute</h2>;
}
function App() {
return <Welcome />;
}
export default App;
Welcome to CIIT Training Institute
Console on initial mount:
Welcome component mounted
03 Component Lifecycle Stages
A React component can pass through three important stages: mounting, updating and unmounting.
Mounting
The component is created and displayed.
Updating
The component renders again when relevant state or props change.
Unmounting
The component is removed from the UI and cleanup can run.
useEffect() determine
when that effect runs again.
04 Mounting Phase
Mounting occurs when React adds a component to the UI for the first time. In this example, an effect with an empty dependency array runs after the component commits.
import { useEffect } from "react";
function CourseWelcome() {
useEffect(() => {
console.log("CourseWelcome mounted");
}, []);
return (
<section>
<h2>React JS Course</h2>
<p>Welcome to CIIT Training Institute.</p>
</section>
);
}
export default CourseWelcome;
React JS Course
Welcome to CIIT Training Institute.
Console on mount:
CourseWelcome mounted
05 Updating Phase with useEffect()
A component can update when its state or props change.
The following example uses useState() to update
a student's progress and useEffect() to observe
changes to that value.
import { useEffect, useState } from "react";
function StudentProgress() {
const [progress, setProgress] = useState(0);
useEffect(() => {
console.log("Progress updated:", progress);
}, [progress]);
return (
<section>
<h2>Samadhan's Learning Progress</h2>
<p>Progress: {progress}%</p>
<button onClick={() => setProgress(progress + 10)}>
Increase Progress
</button>
</section>
);
}
export default StudentProgress;
Samadhan's Learning Progress
Progress: 0%
Click the button in the running React application to increase progress by 10%. The effect logs each updated value.
06 Unmounting Phase
Unmounting occurs when a component is removed from the UI.
A cleanup function returned by useEffect() can
run when the component unmounts.
import { useEffect, useState } from "react";
function StudentMessage() {
useEffect(() => {
console.log("StudentMessage mounted");
return () => {
console.log("StudentMessage unmounted");
};
}, []);
return <p>Samiksha is learning React JS.</p>;
}
function App() {
const [showMessage, setShowMessage] = useState(true);
return (
<section>
<button onClick={() => setShowMessage(!showMessage)}>
Toggle Student Message
</button>
{showMessage && <StudentMessage />}
</section>
);
}
export default App;
Initially visible:
Samiksha is learning React JS.
After hiding the message, the paragraph disappears.
Console on unmount:
StudentMessage unmounted
07 Understanding the useEffect Dependency Array
The dependency array controls when an effect is re-run. Compare these three common patterns.
No Dependency Array
useEffect(() => { /* effect */ });
The effect runs after every committed render.
Empty Dependency Array
useEffect(() => { /* effect */ }, []);
The effect runs after the initial mount, with cleanup when needed.
With Dependencies
useEffect(() => { /* effect */ }, [count]);
The effect re-runs when a dependency changes.
08 Practical Example: Student Dashboard
This example combines mounting and updating. When the student changes the selected course, the effect logs the latest course.
import { useEffect, useState } from "react";
function StudentDashboard() {
const [course, setCourse] = useState("React JS");
useEffect(() => {
console.log("Selected course:", course);
}, [course]);
return (
<section>
<h2>CIIT Student Dashboard</h2>
<p>Student: Pradnya</p>
<p>Selected Course: {course}</p>
<button onClick={() => setCourse(".NET Full Stack")}>
Select .NET Full Stack
</button>
</section>
);
}
export default StudentDashboard;
CIIT Student Dashboard
Student: Pradnya
Selected Course: React JS
After selecting another course:
Selected Course: .NET Full Stack
The React application updates the displayed course and logs the new selection in the browser console.
09 Important Rules and Best Practices
Use useEffect() for side effects, not for ordinary rendering calculations.
Include every reactive value used by an effect in its dependency array.
Return a cleanup function when subscriptions, timers or listeners need cleanup.
Do not assume an effect runs only once in every development configuration.
Keep component lifecycle logic simple, readable and maintainable.
10 Practice Questions
- What is the React component lifecycle?
- Explain mounting, updating and unmounting.
- What is the purpose of the
useEffect()Hook? - What does an empty dependency array mean?
- Why is a cleanup function useful?
- Create a student component that logs changes to its course.
Summary
The React Component Lifecycle describes how components are mounted, updated and unmounted. Functional components commonly use the useEffect() Hook to handle side effects and cleanup. Its dependency array determines when the effect runs again. Understanding these stages helps developers build reliable, maintainable interfaces for student dashboards and course pages at CIIT Training Institute.