Props
React props kya hain, parent se child component tak data kaise jata hai, destructuring aur default values, Roman Urdu mein examples ke saath.
On this page
1. Props kya hain?
React mein component ek function hai. Jaise function ko arguments dete hain, waise hi component ko props dete hain. Props ka poora naam "properties" hai.
Props ki madad se ek hi component alag alag data ke saath dobara use ho sakta hai. Ek ProductCard component, sau products.
2. Kyun zaroori hai?
Agar props na hon to har product ke liye alag component likhna parega. Props component ko reusable banate hain.
| Bina props | Props ke saath |
|---|---|
| Har card ke liye alag component | Ek component, data bahar se aata hai |
| Text component ke andar hardcoded | Text parent decide karta hai |
| Change ke liye har jagah edit | Sirf data badlo, UI khud update |
| Copy paste code | Clean aur reusable code |
3. Kahan use hota hai?
| Use case | Props ka kaam |
|---|---|
| Product card | name, price, image pass karna |
| Button | label, variant, onClick pass karna |
| User avatar | src, size, alt pass karna |
| Layout | title aur children pass karna |
4. Kaise kaam karta hai?
Parent JSX mein attributes likhta hai. React in sab ko ek object mein jama karke child function ko pehla argument bana kar deta hai.
Data sirf upar se neeche jata hai (parent se child). Isay one-way data flow kehte hain.
5. Code example
Basic props
function Greeting(props) {
return <h2>Assalam o Alaikum, {props.name}!</h2>;
}
export default function App() {
return (
<>
<Greeting name="Ali" />
<Greeting name="Sara" />
</>
);
}Destructuring aur default value
function ProductCard({ name, price, inStock = true }) {
return (
<div className="card">
<h3>{name}</h3>
<p>Rs. {price}</p>
<p>{inStock ? "Available" : "Out of stock"}</p>
</div>
);
}
export default function App() {
return (
<>
<ProductCard name="Keyboard" price={4500} />
<ProductCard name="Mouse" price={1800} inStock={false} />
</>
);
}Code kaise kaam kar raha hai?
| Cheez | Matlab |
|---|---|
name="Ali" | String prop, quotes mein |
price={4500} | Number ya koi bhi JS value, curly braces mein |
{ name, price } | Props object ko seedha destructure karna |
inStock = true | Default value, jab parent ne prop na diya ho |
inStock={false} | Boolean prop explicitly false |
Strings ke ilawa har value (number, boolean, array, object, function) {} ke andar pass hoti hai.
6. Common mistakes
Galati: Child component ke andar props ko change karna
Why? Props read-only hain. Unhein mutate karne se UI aur data out of sync ho jate hain aur bugs aate hain.
Behtar: Value badalni ho to parent mein state rakho aur child ko ek function prop do jo parent ki state update kare.
Galati: Number ko quotes mein pass karna, jaise price ko string bana dena
Why? Quotes wali value hamesha string hoti hai, phir math ya comparison galat result deta hai.
Behtar: Number, boolean aur objects ko curly braces mein pass karo, jaise price={4500}.
Galati: Destructuring mein curly braces bhool jana
Why? function Card(name) likhne se name poora props object ban jata hai, sirf name nahi.
Behtar: function Card({ name }) likho taake props object se sirf name nikle.
Interview tip
Agar poocha jaye "Props aur state mein kya farq hai?" to jawab: props bahar se aate hain aur read-only hain, state component ki apni memory hai jo woh khud update karta hai.
7. Pehle aur baad mein kya seekhein?
| Stage | Chapters |
|---|---|
| Pehle | Components, JSX |
| Saath mein | Children & Composition |
| Baad mein | State with useState, Lifting State Up |
Final quick revision
| Point | Detail |
|---|---|
| Props kya hain | Parent se child ko diye gaye inputs |
| Shape | Ek object, pehla function argument |
| Direction | Sirf upar se neeche (one-way) |
| Rule | Child props ko change nahi karta |
| Default value | Destructuring mein = value |