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:
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:
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.
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:
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:
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.