ReactJS | Props – Set 1

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 details 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 a 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

import React from 'react';
import ReactDOM from 'react-dom';
  
// sample component to illustrate props
class 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")
);

chevron_right


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 class-based component.

filter_none

edit
close

play_arrow

link
brightness_4
code

import React from 'react';
import ReactDOM from 'react-dom';
  
// functional component to illustrate props
function 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")
);

chevron_right


The output of this program will be same as that of the above program. The only difference is that we have used a function-based component instead of 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

import React from 'react';
import ReactDOM from 'react-dom';
  
// Parent Component
class Parent extends React.Component{
    render(){
        return(
                <div>
                    <h2>You are inside Parent Component</h2>
                    <Child name="User" userId = "5555"/>
                </div>
            );
    }
}
  
// Child Component
class 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")
);

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

import React from 'react';
import ReactDOM from 'react-dom';
  
// Parent Component
class Parent extends React.Component{
    render(){
        return(
                <div>
                    <h2>You are inside Parent Component</h2>
                    <Child name="User" userId = "5555"/>
                </div>
            );
    }
}
  
// Child Component
class 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")
);

chevron_right



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 next article.

Reference:
https://reactjs.org/docs/components-and-props.html



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.