Props
Props (short for properties) React mein ek fundamental concept hai jo components ke data ko pass karne aur share karne mein madad karta hai. Props ek way hai parent component se child component tak data ko transmit karne ka.
Props Ka Istemal Kaise Hota Hai?
React mein, parent component apne child component ko props ke roop mein data provide karta hai. Props immutable (unchangeable) hote hain, yaani ki child component props ko directly change nahi kar sakta, sirf read kar sakta hai.
Props Ka Syntax
Props ko child component tak pass karne ke liye, parent component ke JSX tag ke andar attributes ke roop mein pass kiya jata hai. Example dekhte hain:
Is example mein, ParentComponent
ChildComponent
ko name
aur age
props ke roop mein data pass kar raha hai. ChildComponent
props ko receive karta hai aur render karta hai.
Props Ka Benefit
- Reusability: Props components ko reusable banata hai kyunki data ko parent component se child component tak easily transmit kiya ja sakta hai.
- Component Communication: Props ke through components ke beech communication hoti hai, jo ki React applications ko modular aur maintainable banata hai.
- Dynamic UI: Props ka use karke dynamic data ko display karna easy hota hai, jaise ki user information, list items, etc.
Props Ke Type
-
Strings: Props strings ke roop mein pass kiya ja sakta hai.
-
Numbers: Props numbers ke roop mein pass kiya ja sakta hai.
-
Boolean: Props boolean values ke roop mein pass kiya ja sakta hai.
-
Functions: Props functions ke roop mein pass kiya ja sakta hai, jo child component mein use kiya ja sakta hai.
-
Objects: Props objects ke roop mein pass kiya ja sakta hai.
Default Props
Components mein default props bhi define kiye ja sakte hain jo agar parent component se specific prop pass nahi kiya gaya ho, tab use hote hain. Example:
Is example mein, agar ParentComponent
name
aur age
props nahi pass karta hai to ChildComponent
default values use karega (name: 'Guest'
, age: 18
).
Conclusion
Props React mein component communication aur data transfer ka ek powerful mechanism hai. Props components ko reusable banata hai aur dynamic data ko manage karne mein madad karta hai. Props immutable nature se ensure karta hai ki data flow predictable aur maintainable rahe, jo React applications ke development ko streamline karta hai.