Back to Top

What are props and how use in react components.

Updated 10 April 2023

Defination

Props basically just like properties that pass from one component to another component by using HTML attributes.It is an object which stores the value of attributes of a tag and work similar to the HTML attributes. It is similar to function arguments. Props are passed to the component in the same way as arguments passed in a function.
In react use the two types of component class and functional component.

Before introducing the functional components react used only class components. In react 16.8 introduce the hooks features that allow you to use state without writing class components. Instead of the class component, we will use the functional component because it’s easy to use and easy to understand.

Uses

Component App.js

import React from 'react';
import TestComponent from './TestComponent';

function App() {
return (
<div>
<div className="container">
<TestComponent name="Deepak" />
</div>
</div>
);
}

A property ‘name’ like attribute in ‘TestComponent’ just like this-
<TestComponent name=”Deepak” />
Here prop is ‘name’ and now we can use this property in “TestComponent”.These props just return an object of properties and we use like “props.name” to access the name properties into the “TestComponent”

Use props in component

Start your headless eCommerce
now.
Find out More

TestComponent.js

import React  from "react";

export default function TestComponent(props) {
return (
<div className="register-form">
<p className="text-center">My Name is { props.name}</p>
</div>
)
}

Simply access the name “{props.name}” and the result be “My name is Deepak”

We can pass multiple values in components using attributes like-

<TestComponent name=”Rama” address=”Noida” /> and access same as props.property_name.

Output-

The front output of props example

Send state/props to another component onClick event

State: The state is used to change the component state from within. Changes to the state also trigger a UI update like onclick, onchange, and other events.
We store the state/props into the parent component i.e in which component where we trigger the onClick event. Then to pass the state into another component, we simply pass it as a prop.
App.js

import React  {useState} from 'react';
import TestComponent from './TestComponent';

function App() {
const [state, setstate] = useState({data:""})
const changeState = () => {  
setstate({data:`Hey! i am a software developer`}); 
};

return (
<div>
<div className="container">
<TestComponent data={state.data} />
<button  onClick={changeState} type="button">
 Click me 
</button>
</div>
</div>
);
}

Component TestComponent.js

import React from 'react';

export default function TestComponent(props)  {
return(
<div className="main-cointainer">
<h2>TestComponent</h2> 
<p>{props.data} </p>
</div>
)
}

Conclusion-
Props for “Properties.” They are read-only components. props are an object which stores the value of attributes of a tag and work similarly to the HTML attributes. It gives a way to pass data from one component to another components. It is similar to function arguments. Props are passed to the component in the same way as arguments passed in a function.

For more details, you can follow the document. See the blog click here

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home