ReactJS Props – Set 1
We have already discussed Components and their 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 (which stands for properties). Props are objects which can be used inside a component. 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 component’s 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 below example, we will use a class-based component to illustrate the props. But we can also pass props to function-based components and going to pass in the below example. 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. let’s see the example.
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 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" ) ); |
Output: This will be the output of the above program.
Below is the same example as above but this time using a function-based component instead of a class-based component. 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.
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 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" ) ); |
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 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" ) ); |
Output: See the output of this program
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 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" ) ); |
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 detail in the next article Reactjs | props set-2
Reference:
https://reactjs.org/docs/components-and-props.html
Please Login to comment...