Components
React components kya hain, function component kaise banate hain, naming rules, export aur components ko jor kar UI banana Roman Urdu mein.
On this page
1. Components kya hain?
Har button, card, navbar aur poora page ek component ho sakta hai. Components ko Lego blocks ki tarah jor kar poori app banti hai. Modern React mein sirf function components likhe jate hain, class components purana tareeqa hain.
2. Kyun zaroori hain?
Socho aapki site par 20 jagah ek jaisa product card hai. Bina components ke har jagah wahi HTML copy hoga aur ek design change ke liye 20 jagah edit karna parega.
| Bina components | Components ke saath |
|---|---|
| Wahi markup baar baar copy | Ek baar likho, har jagah use karo |
| Ek change ke liye kai files edit | Ek component badlo, har jagah update |
| Ek bari file mein saara UI | Chhote hisson mein saaf code |
| Test karna mushkil | Har component alag test ho sakta hai |
3. Kahan use hote hain?
| Level | Example components |
|---|---|
| Chhote UI pieces | Button, Avatar, Badge, Input |
| Sections | Navbar, ProductCard, CommentList |
| Pages | HomePage, DashboardPage |
| Layouts | Sidebar ke saath AppLayout |
| Libraries | shadcn/ui, MUI jaise ready components |
4. Kaise kaam karte hain?
React aap ke component function ko call karta hai, woh JSX return karta hai, aur React us JSX ko screen par dikhata hai. Ek component doosre components ko use karta hai, is se ek component tree banta hai.
Component ke rules
| Rule | Wajah |
|---|---|
| Naam Capital letter se | Chhote naam ko React HTML tag samajhta hai |
| JSX return kare | Ya null agar kuch nahi dikhana |
| Pure ho | Same input par same output, render mein side effects nahi |
| Top level par define ho | Component ke andar component define na karein |
5. Code example
Component banana aur export
// src/components/Button.jsx
export function Button({ label }) {
return <button className="btn">{label}</button>;
}// src/components/ProductCard.jsx
import { Button } from "./Button.jsx";
export function ProductCard({ name, price }) {
return (
<article className="card">
<h3>{name}</h3>
<p>Rs. {price}</p>
<Button label="Cart mein daalein" />
</article>
);
}Components ko jorna
// src/App.jsx
import { ProductCard } from "./components/ProductCard.jsx";
export default function App() {
return (
<main>
<h1>Hamari Shop</h1>
<ProductCard name="Keyboard" price={4500} />
<ProductCard name="Mouse" price={1800} />
</main>
);
}Code kaise kaam kar raha hai?
| Cheez | Matlab |
|---|---|
function Button(...) | Component sirf ek function hai |
{ label } | Props ka destructuring, parent se data aata hai |
export function | Named export, doosri file mein import ho sakta hai |
<Button label="..." /> | Component ko tag ki tarah use karna |
price={4500} | Number ya JavaScript value braces mein |
Do ProductCard | Ek hi component, alag data, alag output |
Props ki poori detail Props chapter mein hai.
Interview tip
"Component aur element mein farq?" Component ek function hai (ProductCard). Element woh object hai jo JSX likhne se banta hai (<ProductCard />), yani React ko batana ke yeh component render karo.
6. Common mistakes
Galati: Component ka naam chhote letter se rakhna
Why? React button ko HTML ka button tag samajhta hai, aap ka component render nahi hota.
Behtar: Hamesha PascalCase naam rakhein, jaise ProductCard.
Galati: Component ke andar doosra component define karna
Why? Har render par naya component banta hai, us ki state reset ho jati hai aur performance kharab hoti hai.
Behtar: Har component ko file ke top level par define karein.
Galati: Component function ko khud call karna
Why? ProductCard() likhne se React usay component nahi samajhta, hooks aur state theek se kaam nahi karte.
Behtar: Hamesha JSX tag ki tarah use karein, jaise ProductCard ko angle brackets mein.
Galati: Ek component mein saari app likhna
Why? File lambi, mushkil aur reuse ke qabil nahi rehti.
Behtar: Jab koi hissa repeat ho ya alag kaam kare to usay naya component bana dein.
7. Pehle aur baad mein kya seekhein?
| Stage | Topics |
|---|---|
| Pehle | JSX, Functions, Modules |
| Saath | How React Renders |
| Baad mein | Props, Children & Composition, State with useState |
Final quick revision
| Point | Detail |
|---|---|
| Component | Function jo JSX return kare |
| Naam | PascalCase, jaise UserCard |
| Use | <UserCard /> tag ki tarah |
| Data | Props ke through parent se |
| Structure | Components se component tree |
| Purana tareeqa | Class components, naye code mein nahi |