0% found this document useful (0 votes)
9 views10 pages

4.4 Properties

Uploaded by

santhini.t
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

4.4 Properties

Uploaded by

santhini.t
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

DEPARTMENT NAME: MASTER OF COMPUTER APPLICATIONS

Class: I MCA Batch: 2023-2025


Course code:23CAR309 Course Title: FULL STACK
WEBDEVELOPMENT

UNIT IV ADVANCED CLIENT SIDE PROGRAMMING


React JS: ReactDOM - JSX - Components - Properties – Fetch API - State and Lifecycle - JS
Local storage - Events - Lifting State Up - Composition and Inheritance.

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.

Prepared By T.Santhini AP/MCA


DEPARTMENT NAME: MASTER OF COMPUTER APPLICATIONS

Class: I MCA Batch: 2023-2025


Course code:23CAR309 Course Title: FULL STACK
WEBDEVELOPMENT

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

import PropTypes from 'prop-types';


import React from 'react';
import ReactDOM from 'react-dom/client';

// Component
class ComponentExample extends React.Component{
render(){
return(
<div>

{/* printing all props */}


<h1>

Prepared By T.Santhini AP/MCA


DEPARTMENT NAME: MASTER OF COMPUTER APPLICATIONS

Class: I MCA Batch: 2023-2025


Course code:23CAR309 Course Title: FULL STACK
WEBDEVELOPMENT

{this.props.arrayProp}
<br />
{this.props.stringProp}
<br />
{this.props.numberProp}
<br />
{this.props.boolProp}
<br />
</h1>
</div>
);
}
}

// Validating prop types


ComponentExample.propTypes = {
arrayProp: PropTypes.array,
stringProp: PropTypes.string,
numberProp: PropTypes.number,
boolProp: PropTypes.bool,
}

// Creating default props


ComponentExample.defaultProps = {

arrayProp: ['Ram', 'Shyam', 'Raghav'],


stringProp: "GeeksforGeeks",
numberProp: "10",

Prepared By T.Santhini AP/MCA


DEPARTMENT NAME: MASTER OF COMPUTER APPLICATIONS

Class: I MCA Batch: 2023-2025


Course code:23CAR309 Course Title: FULL STACK
WEBDEVELOPMENT

boolProp: true,
}

const root = ReactDOM.createRoot(document.getElementById("root"));


root.render(
<React.StrictMode>
<ComponentExample />
</React.StrictMode>
);

Passing and Accessing props


We can pass props to any component as we declare attributes for any HTML tag.
Syntax:
// Passing Props
<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.
Syntax:
// Accessing props
this.props.propName;
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.

Prepared By T.Santhini AP/MCA


DEPARTMENT NAME: MASTER OF COMPUTER APPLICATIONS

Class: I MCA Batch: 2023-2025


Course code:23CAR309 Course Title: FULL STACK
WEBDEVELOPMENT

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';

// 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>
);
}
}

const root = ReactDOM.createRoot(document.getElementById("root"));


root.render(
<React.StrictMode>
<DemoComponent />
</React.StrictMode>
);

Prepared By T.Santhini AP/MCA


DEPARTMENT NAME: MASTER OF COMPUTER APPLICATIONS

Class: I MCA Batch: 2023-2025


Course code:23CAR309 Course Title: FULL STACK
WEBDEVELOPMENT

Output: This will be the output of the above program

gfg

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';
// 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>
);
}
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
<DemoComponent user="Geeks"/>
</React.StrictMode>
);
Output:gfg

Prepared By T.Santhini AP/MCA


DEPARTMENT NAME: MASTER OF COMPUTER APPLICATIONS

Class: I MCA Batch: 2023-2025


Course code:23CAR309 Course Title: FULL STACK
WEBDEVELOPMENT

Passing information from one component to another


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

Prepared By T.Santhini AP/MCA


DEPARTMENT NAME: MASTER OF COMPUTER APPLICATIONS

Class: I MCA Batch: 2023-2025


Course code:23CAR309 Course Title: FULL STACK
WEBDEVELOPMENT

</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>

Prepared By T.Santhini AP/MCA


DEPARTMENT NAME: MASTER OF COMPUTER APPLICATIONS

Class: I MCA Batch: 2023-2025


Course code:23CAR309 Course Title: FULL STACK
WEBDEVELOPMENT

<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>
);
}
}
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.

Prepared By T.Santhini AP/MCA


DEPARTMENT NAME: MASTER OF COMPUTER APPLICATIONS

Class: I MCA Batch: 2023-2025


Course code:23CAR309 Course Title: FULL STACK
WEBDEVELOPMENT

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.

Prepared By T.Santhini AP/MCA

You might also like