Props in React

Subject: React

What are Props?

Props (short for properties) are a way of passing data from one component to another in React โ€” specifically from a parent to a child component.

They allow you to make components reusable and dynamic, by feeding them custom data that they can render or use in logic.


๐Ÿ”ง Why Use Props?

  • To make components flexible and reusable
  • To communicate between components
  • To customize behavior or content

๐Ÿ” How Props Work (Concept Flow)


๐Ÿงฑ Basic Example

๐Ÿงฉ Parent Component:

๐Ÿงฉ Child Component (Greeting.js):

๐Ÿงพ Output:


๐Ÿ”ง Accessing Props

Props are passed as an object to the component:

You can also destructure for cleaner syntax:


๐Ÿง  Passing Multiple Props


๐Ÿ” Props are Read-Only

You cannot change a prop inside a component. If you try to do this:

Props are meant to receive data, not modify it.


๐Ÿงช Dynamic Rendering with Props

You can pass props in loops:


๐ŸŽฏ Real-Life Analogy

Think of a component as a function, and props as its arguments:

Similarly:


๐Ÿง  Best Practices

  • โœ… Always keep props immutable
  • โœ… Use destructuring for cleaner syntax
  • โœ… Use PropTypes or TypeScript to enforce prop types (optional)

๐Ÿ› ๏ธ BONUS: Default Props (Optional Values)


๐Ÿ”š Summary

FeatureExplanation
What are Props?Data passed from parent to child
DirectionUnidirectional (Top to Bottom)
Syntax<Component propName="value" />
AccessVia props.propName or destructuring
Modifiable?โŒ No, they are read-only
PurposeMake components reusable and dynamic