How to create Class Component in React?
Last Updated :
28 Apr, 2025
JavaScript syntax extension permits HTML-like code within JavaScript files, commonly used in React for defining component structure. It simplifies DOM element manipulation by closely resembling HTML syntax. JSX facilitates the creation of reusable UI building blocks, defined as JavaScript functions or classes in React.
Prerequisites:
Steps to Create React Application And Installing Module:
Step 1: Create a React application using the following command:
npx create-react-app foldername
Step 2: After creating your project folder i.e. foldername, move to it using the following command:
cd foldername
Step 3: After setting up React environment on your system, we can start by creating an App.js file and create a directory by the name of components in which we will write our desired function.
Project Structure:

The updated dependencies in package.json file will look like.
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4",
}
Example 1: In this example, we will demonstrate how to add a class component in our application.
JavaScript
//App.js
import GeeksforGeeks
from "./components/geeksforgeeks";
import "./styles.css";
export default function App() {
return (
<div className="App">
{
/* Creating custom JSX tag
to return the component */
}
<GeeksforGeeks />
</div>
);
}
JavaScript
//GeeksforGeeks.js
import React from "react";
/* This is the class component that will be
added to the application */
class GeeksforGeeks
extends React.Component {
render() {
return (
<div>
<h2>GeeksforGeeks</h2>
<p>
A computer science
portal for GeeksforGeeks
</p>
</div>
);
}
}
export default GeeksforGeeks;
Step to run the app:
npm start
Output:

Example 2: In this example, we will demonstrate how to add a class component in our application with a custom input by changing the contents of the header in the App.js file.
JavaScript
//App.js
import GeeksforGeeks
from "./components/geeksforgeeks";
import "./styles.css";
export default function App() {
return (
<div className="App">
{
/* Creating custom JSX
tags with different inputs */
}
<GeeksforGeeks data="GeeksforGeeks" />
<GeeksforGeeks data="A computer science portal" />
<GeeksforGeeks data="for geeks" />
</div>
);
}
JavaScript
//GeeksforGeeks.js
import React from "react";
/* This is the class component that will be
added to the application with custom inputs */
class GeeksforGeeks
extends React.Component {
render() {
return (
<div>
<p>{this.props.data}</p>
</div>
);
}
}
export default GeeksforGeeks;
Step to run the app:
npm start
Output:

Similar Reads
How to create components in ReactJS ? Components in React JS is are the core of building React applications. Components are the building blocks that contains UI elements and logic, making the development process easier and reusable. In this article we will see how we can create components in React JS. Table of Content React Functional C
3 min read
How to create a Functional Component in React? To create a functional component in React, you define a JavaScript function that returns JSX. These components are stateless and rely on props for data input. Functional components are concise, easy to read, and can utilize React Hooks for managing state and side effects, making them a fundamental b
2 min read
How to create Functional Components in React ? To create a functional component in React, you define a JavaScript function that returns JSX. These components are stateless and rely on props for data input. Functional components are concise, easy to read, and can utilize React Hooks for managing state and side effects, making them a fundamental b
2 min read
How to create Menu Item Component in ReactJS ? React JS is a popular JavaScript library used for building user interfaces. It provides a component-based architecture that allows developers to create reusable UI elements. Material UI for React has this component available for us and it is very easy to integrate. We can use Menu Component in React
2 min read
What are Class Components in React? Class components in React are fundamental building blocks for creating reusable, stateful, and interactive user interfaces. Unlike functional components, class components can manage their own state and lifecycle methods, making them a preferred choice for more complex applications before the introdu
3 min read