React Props Explained with Greeting Component Example
π INTRODUCTION
In React, props (short for properties) are used to pass data from one component to another.
They are:
- Read-only
- Used for communication between components
- Essential for building reusable UI
π In this program, we will create a Greeting Component using props.
π PROBLEM STATEMENT
π Create a React component that takes a name prop and displays a greeting message.
π‘ CONCEPTS USED
- React Props
- Functional Components
- Component Reusability
- JSX
π§ LOGIC EXPLANATION
- Create a component
- Pass data using props
- Access props inside component
- Display dynamic message
π» PROGRAM CODE
Step 1: Create Greeting Component
function Greeting(props) {
return <h2>Hello, {props.name}!</h2>;
}
export default Greeting;
Step 2: Use Component in App.js
import Greeting from "./Greeting";
function App() {
return (
<div>
<h1>Welcome App</h1>
<Greeting name="Kanchan" />
<Greeting name="Amit" />
<Greeting name="Riya" />
</div>
);
}
export default App;
π₯οΈ OUTPUT
Welcome App Hello, Kanchan! Hello, Amit! Hello, Riya!
π STEP-BY-STEP EXPLANATION
<Greeting name="Kanchan" />β passing dataprops.nameβ accessing data- Same component reused multiple times
- Output changes dynamically
β‘ MODERN WAY (DESTRUCTURING PROPS)
function Greeting({ name }) {
return <h2>Hello, {name}!</h2>;
}
π Cleaner and widely used approach.
π― WHY PROPS ARE IMPORTANT
- Enable component communication
- Make components reusable
- Help build scalable applications
- Core concept in React
π ADVANCED EXAMPLES
1. Multiple Props
<Greeting name="Kanchan" course="MCA" />
function Greeting({ name, course }) {
return <h2>Hello {name}, Course: {course}</h2>;
}
2. Default Props
function Greeting({ name = "Guest" }) {
return <h2>Hello, {name}!</h2>;
}
π REAL-WORLD USE CASE
- Passing data to components
- Displaying user information
- Building reusable UI components
- Handling dynamic content
β VIVA QUESTIONS (IMPORTANT)
Q1. What are props in React?
π Props are used to pass data from parent to child components.
Q2. Are props mutable?
π No, props are read-only.
Q3. What is destructuring in props?
π Extracting values directly from props object.
Q4. Can we pass multiple props?
π Yes, we can pass multiple values.
Q5. Difference between props and state?
π Props are passed, state is managed internally.
π RELATED POSTS
π MCA 168 Full Stack Development Lab Programs List
π React Counter App β Program 6
π React List Rendering β Program 7
π React Toggle Visibility β Program 8
π React Background Color β Program 9
π CONCLUSION
Props are the backbone of React applications. They allow components to communicate and make your UI dynamic and reusable.
π Master props to build real-world React applications effectively.
Further Reading
How to Master Full Stack Development?
Spring Framework Practice Problems and Their Solutions
From Google to the World: The Story of Go Programming Language
Why Go? Understanding the Advantages of this Emerging Language
Creating and Executing Simple Programs in Go
20+ Interview Questions on Go Programming Language
100+ MCQs On Java Architecture
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
