Skip to content

Props Overview

In React, props (short for properties) are:

👉 Inputs passed from a parent component to a child component

They allow components to be dynamic and reusable.


Think of a component like a function:

function greet(name) {
return "Hello " + name;
}

👉 name = input (like a prop)

Similarly in React:

function Greeting(props) {
return <h1>Hello {props.name}</h1>;
}

👉 props.name = the input passed from outside


Props flow from top → bottom through the component tree. Children cannot modify their props directly.

flowchart TB
App["🏗️ App\nRoot Component"] --> Header["📦 Header\nprops: logo, title"]
App --> Sidebar["📦 Sidebar\nprops: menuItems, isOpen"]
App --> Content["📦 Content\nprops: data"]
Header --> Logo["🪆 Logo\nprops: src, alt"]
Content --> Card["🪆 ProductCard\nprops: name, price, onBuy"]
Content --> Card2["🪆 ProductCard\nprops: name, price, onBuy"]
style App fill:#7c3aed,color:#fff
style Header fill:#4f46e5,color:#fff
style Sidebar fill:#4f46e5,color:#fff
style Content fill:#4f46e5,color:#fff
style Logo fill:#059669,color:#fff
style Card fill:#059669,color:#fff
style Card2 fill:#059669,color:#fff

📌 Props flow ONE-WAY: top → bottom. Parent components pass data down. Children receive and render it.


function App() {
return <Greeting name="Prathamesh" />;
}
function Greeting(props) {
return <h1>Hello {props.name}</h1>;
}

Output: Hello Prathamesh


function User({ name, age }) {
return <h1>{name} is {age} years old</h1>;
}
<User name="Prathamesh" age={25} />

function Greeting(props) {
props.name = "Changed"; // ❌ Not allowed!
}

👉 Props are immutable. You cannot modify them inside the child component. If you need to change data, use state instead.


PropsState
Passed from parentManaged inside component
Read-onlyMutable (via setter)
External dataInternal data
Cannot changeTriggers re-render on change

function ProductCard({ name, price, onBuy }) {
return (
<div>
<h2>{name}</h2>
<p>₹{price}</p>
<button onClick={onBuy}>Buy</button>
</div>
);
}
function App() {
function handleBuy() { alert("Purchased!"); }
return (
<ProductCard
name="Laptop"
price={50000}
onBuy={handleBuy}
/>
);
}

MistakeWhyFix
age="25" instead of age={25}Passes string instead of numberUse {} for JS values
props.name = "X"Props are immutableUse state for mutable data
Drilling props through 5+ layersMakes code hard to maintainUse Context API or state management


  • Props = inputs passed from parent to child (like function arguments)
  • Props flow one-way: top → bottom. Children can’t modify them directly
  • Props are read-only — never try to change them inside the child
  • Use state for data that changes inside a component
  • Check the sub-pages above for deep dives on each topic