Skip to content
Getting StartedChapter 4 / 37Beginner4 min read

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 componentsComponents ke saath
Wahi markup baar baar copyEk baar likho, har jagah use karo
Ek change ke liye kai files editEk component badlo, har jagah update
Ek bari file mein saara UIChhote hisson mein saaf code
Test karna mushkilHar component alag test ho sakta hai

3. Kahan use hote hain?

LevelExample components
Chhote UI piecesButton, Avatar, Badge, Input
SectionsNavbar, ProductCard, CommentList
PagesHomePage, DashboardPage
LayoutsSidebar ke saath AppLayout
Librariesshadcn/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 tree
App
Header + ProductList + Footer
ProductList ke andar kai ProductCard
Har ProductCard ke andar Button

Component ke rules

RuleWajah
Naam Capital letter seChhote naam ko React HTML tag samajhta hai
JSX return kareYa null agar kuch nahi dikhana
Pure hoSame input par same output, render mein side effects nahi
Top level par define hoComponent ke andar component define na karein

5. Code example

Component banana aur export

jsx
// src/components/Button.jsx
export function Button({ label }) {
  return <button className="btn">{label}</button>;
}
jsx
// 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

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

CheezMatlab
function Button(...)Component sirf ek function hai
{ label }Props ka destructuring, parent se data aata hai
export functionNamed 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 ProductCardEk 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?

Final quick revision

PointDetail
ComponentFunction jo JSX return kare
NaamPascalCase, jaise UserCard
Use<UserCard /> tag ki tarah
DataProps ke through parent se
StructureComponents se component tree
Purana tareeqaClass components, naye code mein nahi