How to Create Class-Based Components in React
How to Create Class-Based Components in React
By Codes With Pankaj
A class-based component in React is an older but still useful way to define components in React. Follow these steps to create and run a class-based component.
Step 1: Create a New React App
If you haven’t already created a React app, follow these steps:
Open your terminal or command prompt.
Run the following command:
This creates a new React app called
my-class-component-app
.
Navigate to the app directory:
Start the development server:
Your browser will open automatically at
http://localhost:3000
with the default React template.
Step 2: Create a Class-Based Component
Inside the
src
folder, create a new file namedWelcome.js
.Add the following code to
Welcome.js
:
Step 3: Use the Class-Based Component in App.js
Open the file
src/App.js
.Import the
Welcome
component at the top of the file:Use the
Welcome
component inside theApp
component:
Step 4: Run the Project
Go back to the terminal and ensure the app is running:
Open your browser at
http://localhost:3000
.You should see the output:
Congratulations! 🎉
You’ve successfully created a class-based component and displayed it in your React app.
Stay tuned for more tutorials from Codes With Pankaj! Let me know if you’d like to expand this tutorial. 😊
Last updated