ReactJS | Props – Set 1

22 Jan, 2021
We have already discussed Components and it’s type in our previous articles on ReactJS | Components. Till now we were working with components using static data only. In this article, we will learn about how we can pass information to a Component.React allows us to pass information to a Component using something called props (stands for properties). Props are basically kind of global variable or object. We will learn about these in detail in this article. 
Passing and Accessing props
We can pass props to any component as we declare attributes for any HTML tag. Have a look at the below code snippet:  
<DemoComponent sampleProp = "HelloProp" />
In the above code snippet, we are passing a prop named sampleProp to the component named DemoComponent. This prop has the value “HelloProp”. Let us now see how can we access these props. We can access any props inside from the component’s class to which the props is passed. The props can be accessed as shown below:  
this.props.propName;
We can access any prop from inside a component’s class using the above syntax. The ‘this.props’ is a kind of global object which stores all of a components props. The propName, that is the names of props are keys of this object.Below is a sample program to illustrate how to pass and access props from a component:  
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
Open your react project and edit the App.js file in the src folder:
src App.js:
javascript
import React from 'react';import ReactDOM from 'react-dom'; // sample component to illustrate propsclass DemoComponent extends React.Component{    render(){        return(                 <div>                    {/*accessing information from props */}                    <h2>Hello {this.props.user}</h2>                    <h3>Welcome to GeeksforGeeks</h3>                </div>            );    }} ReactDOM.render(    // passing props    <DemoComponent user = "Harsh Agarwal" />,    document.getElementById("root"));
Output:  
In the above example, we have used a class-based component to illustrate props. But we can pass props to a function-based component also just like we did in the above example. But to access a prop from a function we do not need to use the ‘this’ keyword anymore. Functional components accept props as parameters and can be accessed directly. Below is the same example as above but this time using a function-based component instead of a class-based component. 
Open your react project and edit the App.js file in the src folder:
src App.js: 
javascript
import React from 'react';import ReactDOM from 'react-dom'; // functional component to illustrate propsfunction DemoComponent(props){    return(        <div>            {/*accessing information from props */}            <h2>Hello {props.user}</h2>            <h3>Welcome to GeeksforGeeks</h3>         </div>    );} ReactDOM.render(    // passing props    <DemoComponent user = "Harsh Agarwal" />,    document.getElementById("root"));
The output of this program will be the same as that of the above program. The only difference is that we have used a function-based component instead of a class-based component. 
Passing information from one component to other
This is one of the coolest features of React. We can make components to interact among themselves. We will consider two components Parent and Children to explain this. We will pass some information as props from our Parent component to the Child component. We can pass as many props as we want to a component.Look at the below code: Open your react project and edit the App.js file in the src folder:
src App.js:
javascript
import React from 'react';import ReactDOM from 'react-dom'; // Parent Componentclass Parent extends React.Component{    render(){        return(                <div>                    <h2>You are inside Parent Component</h2>                    <Child name="User" userId = "5555"/>                </div>            );    }} // Child Componentclass Child extends React.Component{    render(){        return(                <div>                    <h2>Hello, {this.props.name}</h2>                    <h3>You are inside Child Component</h3>                    <h3>Your user id is: {this.props.userId}</h3>                </div>            );    }} ReactDOM.render(    // passing props    <Parent />,    document.getElementById("root"));
Output:  
So we have seen props in React and also have learned about how props are used, how they can be passed to a component, how they are accessed inside a component, and much more. We have used the thing “this.props.propName” very often in this complete scenario to access props. Let us now check what is actually being stored in this. We will console.log “this.props” in the above program inside the child component and will try to observe what is logged into the console. Below is the modified program with console.log() statement: 
Open your react project and edit the App.js file in the src folder:
src App.js: 
javascript
import React from 'react';import ReactDOM from 'react-dom'; // Parent Componentclass Parent extends React.Component{    render(){        return(                <div>                    <h2>You are inside Parent Component</h2>                    <Child name="User" userId = "5555"/>                </div>            );    }} // Child Componentclass Child extends React.Component{    render(){        console.log(this.props);        return(                <div>                    <h2>Hello, {this.props.name}</h2>                    <h3>You are inside Child Component</h3>                    <h3>Your user id is: {this.props.userId}</h3>                </div>            );    }} ReactDOM.render(    // passing props    <Parent />,    document.getElementById("root"));
You can clearly see in the above image that in the console it is shown that the this.props is an object and it contains all of the props passed to the child component. The props name of the child component are keys of this object and their values are values of these keys. So, it is clear now that whatever information is carried to a component using props is stored inside an object.Note: Props are read-only. We are not allowed to modify the content of a prop. Whatever the type of Component is – functional or class-based, none of them is allowed to modify their props. We will learn about this in details in the next article Reactjs | props set-2
Reference: https://reactjs.org/docs/components-and-props.html 
shubhamyadav4
react-js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?
Remove elements from a JavaScript Array
REST API (Introduction)
Node.js fs.readFileSync() Method
How to set the default value for an HTML <select> element ?
How to create footer to stay at the bottom of a Web page?