Open In App

What is the purpose of using super constructor with props argument in ReactJS ?

Last Updated : 04 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

React, an open-source JavaScript library for UI design utilizes JSX to render components. Data is passed from parent to child components through props, which can only be updated by the parent. The `super()` function invokes the constructor of the parent class.

Prerequisites:

Steps to Create the React Application:

Step 1: Create a react application by typing the following command in the terminal.

npx create-react-app project_name

Step 2: Now, go to the project folder i.e. project_name by running the following command.

cd project_name

Project Structure:

Project Structure

The updated dependencies in package.json file will look like:

"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-icons": "^4.12.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4",
}

Example: Let us create a super constructor with props argument. 

Javascript




// App.js
 
import React from "react";
 
class App extends React.Component {
    constructor(props) {
        super(props);
        console.log("Empty props ", this.props);
    }
 
    render() {
        return (
            <div>
                <p>Hello World!</p>
            </div>
        );
    }
}
 
export default App;


Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output:

this.props log 

You can even look through these articles for further clarity:


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads