Skip to content
Core ConceptsChapter 6 / 37Beginner4 min read

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 propsProps ke saath
Har card ke liye alag componentEk component, data bahar se aata hai
Text component ke andar hardcodedText parent decide karta hai
Change ke liye har jagah editSirf data badlo, UI khud update
Copy paste codeClean aur reusable code

3. Kahan use hota hai?

Use caseProps ka kaam
Product cardname, price, image pass karna
Buttonlabel, variant, onClick pass karna
User avatarsrc, size, alt pass karna
Layouttitle 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.

Props ka safar
Parent JSX mein attributes likhta hai
React sab attributes ko ek props object banata hai
Child function props object receive karta hai
Child props ke mutabiq UI return karta hai

Data sirf upar se neeche jata hai (parent se child). Isay one-way data flow kehte hain.

5. Code example

Basic props

jsx
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

jsx
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?

CheezMatlab
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 = trueDefault 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?

Final quick revision

PointDetail
Props kya hainParent se child ko diye gaye inputs
ShapeEk object, pehla function argument
DirectionSirf upar se neeche (one-way)
RuleChild props ko change nahi karta
Default valueDestructuring mein = value