4.4 Properties
4.4 Properties
4.4 PROPERTIES
Issues with passing props
We passed different types of information like integers, strings, arrays, etc. as props to the
components.
We can either create defaultProps or have passed props directly as attributes to the
components.
We do not have a check on what type of values we are getting inside our Component
through props.
For larger Apps, it is always a good practice to validate the data we are getting through
props.
This will help in debugging and also helps in avoiding bugs in the future.
React propTypes
Before the release of React 15.5.0 version propTypes was available in the react package but
in later versions of React have to add a dependency in your project. You can add the
dependency to your project by using the command given below:
npm i prop-types
We can use the propType for validating any data we are receiving from props. But before
using it we will have to import it. Add the below line at the top of your index.js file :
import PropTypes from 'prop-types'
Once we have imported propTypes we are ready to work with them. Just like defaultProps,
propTypes are also objects where keys are the prop names and values are their types.
Syntax:
ComponentClassName.propTypes{
propName1 : PropTypes.string,
propName2 : PropTypes.bool,
propName3 : PropTypes.array,
.
.
propNamen : PropTypes.anyOtherType
}
In the above Syntax, the ComponentClassName is the name of the class of
Component, anyOtherType can be any type that we are allowed to pass as props. For the props
which do not validate the type of data specified by propTypes, a warning on the console will occur.
Let us see a complete program that uses propTypes for validation for a better understanding:
Example: Write the following code in index.js file of your react application
javascript
// Component
class ComponentExample extends React.Component{
render(){
return(
<div>
{this.props.arrayProp}
<br />
{this.props.stringProp}
<br />
{this.props.numberProp}
<br />
{this.props.boolProp}
<br />
</h1>
</div>
);
}
}
boolProp: true,
}
Let us see an example where we will implement passing and accessing props.
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.
Open your react project and edit the index.js file in the src folder:
Example: Write the following code in your index.js file.
import React from 'react';
import ReactDOM from 'react-dom/client';
gfg
Open your react project and edit the index.js file in the src folder:
// 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>
);
}
}
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
<Parent/>
</React.StrictMode>
);
Output: See the output of this program
gfg
What is this.props in React?
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 index.js file in the src folder:
Example: Write the following code in your index.js file.
import React from 'react';
import ReactDOM from 'react-dom/client';
// Parent Component
class Parent extends React.Component{
render(){
return(
<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>
);
}
}
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
<Parent/>
</React.StrictMode>
);
Output:gfg
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.