In ReactJS, props (short for properties) are used to pass data from a parent component to a child component. This concept allows components to be reusable and dynamic by feeding them different values. Props work similarly to function parameters β they are read-only and help in communication between components.
Letβs say you have a parent component called App and a child component called Greeting. You can pass the userβs name from the parent to the child using props. This enables the child to display dynamic content without managing its own state. Props are one of the core features of React and are essential for building scalable, modular applications.
In this article, weβll walk through a basic example that demonstrates how to pass and access props in a clean, beginner-friendly way.
π§© Example: Passing Name Prop from Parent to Child
π· Parent Component (App.js)
import React FROM 'react';
import Greeting FROM './Greeting';
FUNCTION App() {
RETURN (
<div STYLE={{ fontFamily: 'Arial', padding: '20px' }}>
<h2>π¨βπ» Welcome TO React Props Demo</h2>
<Greeting userName="Vijay Kumar" />
</div>
);}export DEFAULT App;
πΆ Child Component (Greeting.js)
import React FROM 'react';
FUNCTION Greeting(props) {
RETURN (
<p>Hello, <strong>{props.userName}</strong>! Have a great DAY ahead π</p>
);}export DEFAULT Greeting;
π Key Points:
- Props are passed from parent β child as attributes (userName=”Vijay”)
- Props are read-only and cannot be modified in the child
- You access them using props.propertyName
How to pass data through props in React?
To pass props, add them to the JSX, just like you would with HTML attributes. To read props, use the function Avatar({ person, size }) destructuring syntax. You can specify a default value like size = 100 , which is used for missing and undefined props.
How to pass state as props in React?
Passing state or props from one component to another in React is very simple, especially with the help of event handlers like onClick . Whether you're using class-based components or functional components with hooks, the idea remains the same: state lives in the parent, and data is passed to children via props.
What are props into other components?
Props are arguments passed into React components. Props are passed to components via HTML attributes. props stands for properties.
Related Topics | You May Also Like
|
π Get Free Course β
π Salesforce Administrators π Salesforce Lightning Flow Builder π Salesforce Record Trigger Flow Builder |
π Get Free Course β |