Class Components in React
Understand class-based components, render methods, props and state with practical examples from CIIT Training Institute.
01 Learning Objectives
After completing this lesson, you will be able to:
Define a React component using a JavaScript class.
Return JSX from the render method.
Receive student and course information from a parent.
Manage component data and update the displayed UI.
02 What Is a Class Component?
A Class Component is a React component defined using an ES6
JavaScript class that extends React.Component.
It uses a render() method to return JSX.
React.Component and
implements a render() method.
import React from "react";
class Welcome extends React.Component {
render() {
return (
<div>
<h2>Welcome to CIIT Training Institute!</h2>
<p>Learn React step by step.</p>
</div>
);
}
}
export default Welcome;
import Welcome from "./Welcome";
function App() {
return (
<main>
<Welcome />
</main>
);
}
export default App;
Welcome to CIIT Training Institute!
Learn React step by step.
03 Class Component Flow Diagram
A class component extends React.Component.
Its render method returns JSX, which React uses to display
the component's UI.
import React from "react";
class StudentInfo extends React.Component {
render() {
return (
<div>
<h2>Student Information</h2>
<p>Name: Samadhan</p>
<p>Course: React JS</p>
</div>
);
}
}
export default StudentInfo;
Student Information
Name: Samadhan
Course: React JS
04 Class Components with Props
Props allow a class component to receive data from its parent.
Inside a class component, props are accessed using
this.props.
import React from "react";
class StudentCard extends React.Component {
render() {
return (
<div>
<h3>{this.props.name}</h3>
<p>Course: {this.props.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 State in Class Components
State stores data managed by a component. In class components,
state is initialized in the constructor and updated using
this.setState().
this.state.count = 10. Use
this.setState() to request a state update.
import React from "react";
class Counter extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
}
increaseCount = () => {
this.setState((previousState) => ({
count: previousState.count + 1
}));
};
render() {
return (
<div>
<h2>CIIT Counter</h2>
<p>Count: {this.state.count}</p>
<button onClick={this.increaseCount}>
Increase
</button>
</div>
);
}
}
export default Counter;
CIIT Counter
Count: 0
Initial display: Count is 0. In the actual React app, clicking Increase changes the count to 1, then 2, and so on.
06 Class Component Lifecycle
Class components provide lifecycle methods that run at different stages of a component's life.
componentDidMount()
Runs after the component is mounted.
componentDidUpdate()
Runs after an update is committed.
componentWillUnmount()
Runs before the component is removed.
import React from "react";
class LifecycleDemo extends React.Component {
componentDidMount() {
console.log("Component mounted");
}
componentDidUpdate() {
console.log("Component updated");
}
componentWillUnmount() {
console.log("Component will unmount");
}
render() {
return (
<h2>CIIT Lifecycle Demo</h2>
);
}
}
export default LifecycleDemo;
CIIT Lifecycle Demo
The heading appears in the browser. Lifecycle messages appear in the browser developer console when their corresponding lifecycle stages occur.
Component mounted
07 Functional Components vs Class Components
| Feature | Functional Component | Class Component |
|---|---|---|
| Definition | JavaScript function | JavaScript class |
| UI rendering | Returns JSX | Uses render() |
| State | useState and other Hooks | this.state and this.setState() |
| Props | Function parameters | this.props |
| Lifecycle | Effects and other Hooks | Lifecycle methods |
| Modern React | Recommended for new components | Supported, often used in legacy code |
08 Important Rules and Best Practices
Extend React.Component when defining a class component.
Define a render() method that returns JSX.
Access class component props through this.props.
Update state with this.setState(), not direct assignment.
Use lifecycle methods only when their behavior is needed.
Prefer functional components with Hooks for most new React code.
09 Practice Questions
- Create a class component named
InstituteInfo. - Create a
StudentInfoclass component for Samadhan. - Create a reusable class component that receives name and course props.
- Create a counter using class state and
this.setState(). - Write a component that logs a message in
componentDidMount(). - Explain the difference between functional and class components.
Summary
Class Components are defined using JavaScript classes that extend React.Component and use the render() method to return JSX. They access incoming data through this.props, manage component state with this.state and this.setState(), and support lifecycle methods such as componentDidMount() and componentWillUnmount(). Although class components remain supported, functional components with Hooks are generally preferred for new React development at CIIT Training Institute.