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

  1. Create a component
  2. Pass data using props
  3. Access props inside component
  4. 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 data
  • props.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

JUnit Tutorial

How to Master Full Stack Development?

Spring Framework Practice Problems and Their Solutions

30 MCQs on JUnit

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

Java Practice Exercise

programmingempire

Princites

Leave a Reply

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