React Component Practices
React best practices components aur hooks ke liye, 400 line limit, hooks se named state object, render functions se conditional rendering aur logic component se bahar.
On this page
1. Yeh practice kya hai?
Component ka kaam hai dikhana. State sambhalna hook ka kaam hai, aur rules (kya overdue hai, total kitna hai) src/lib ke pure functions ka. Jab component sirf props aur hook ki values se markup banata hai to woh chhota, parhne layak aur badalne mein mehfooz rehta hai.
2. Kyun zaroori hai?
Teen asli bugs jo yeh rules rokte hain:
- Hook
[invoices, isLoading, error]lautata tha. Ek file mein kisi neconst [invoices, error, isLoading]likh diya.if (isLoading)ab error string par chal raha tha, aur error aane par skeleton hi ghoomta raha. {items.length && <List />}ne khali list par screen par0likh diya, kyunki React0ko render karta hai.- 1200 line ka
Dashboard.tsxkoi review nahi kar paata tha, is liye har PR "LGTM" ho kar merge ho jata tha.
| Bina rules | Rules ke saath |
|---|---|
| Tuple ki tarteeb ghalat, bug chupke se | Named object, naam se value, tarteeb ka masla nahi |
| Nested ternary ko parhna paheli | renderBody mein seedhe if aur return |
| Length aur double ampersand se 0 screen par | Braced guard, sirf element ya null |
| 1000 line ka component, review na mumkin | Chhote child components, apni apni file |
| Rule component mein, test ke liye render | Rule pure function mein, seedha test |
3. Kab follow karein?
| Level | Kitna zaroori | Kyun |
|---|---|---|
| Learning project | Logic hooks mein nikalna aur components chhote rakhna seekhein | Yeh React ki sab se faidemand aadat hai |
| Production code | Charon rules lazmi, review mein check | Team ka code tab hi review hota hai jab chhota aur pehle se andaza layak ho |
| Senior level | Lint rules aur component patterns set karna | react/jsx-no-leaked-render jaise rules review ka bojh kam karte hain |
4. Bura vs acha code
Bura: tuple, ternary aur logic andar
"use client";
export function InvoiceList() {
const [invoices, isLoading, error] = useInvoices();
const overdue = invoices.filter((invoice) => !invoice.paidAt && invoice.dueAt < new Date());
return (
<section>
{overdue.length && <p>{overdue.length} overdue</p>}
{isLoading ? <Spinner /> : error ? <p>{error}</p> : invoices.map((invoice) => (
<InvoiceRow key={invoice.id} invoice={invoice} />
))}
</section>
);
}Acha: named state, render functions, logic bahar
Hook ka result type use-invoices.types.ts mein:
export type UseInvoicesResult = {
invoices: Invoice[];
isLoading: boolean;
error: string | null;
refresh: () => Promise<void>;
};Rule src/lib/invoices/invoices.library.ts mein:
export function isOverdue(invoice: Invoice, now: Date) {
return !invoice.paidAt && invoice.dueAt < now;
}Component sirf jorta aur dikhata hai:
"use client";
export function InvoiceList() {
const { invoices, isLoading, error, refresh } = useInvoices();
const overdueCount = invoices.filter((invoice) => isOverdue(invoice, new Date())).length;
const renderOverdue = () => {
if (overdueCount === 0) {
return null;
}
return <OverdueBadge count={overdueCount} />;
};
const renderBody = () => {
if (isLoading) {
return <InvoiceSkeleton />;
}
if (error) {
return <ErrorState message={error} onRetry={refresh} />;
}
return invoices.map((invoice) => <InvoiceRow key={invoice.id} invoice={invoice} />);
};
return (
<section>
{renderOverdue()}
{renderBody()}
</section>
);
}Farq kya hai?
| Cheez | Bura | Acha |
|---|---|---|
| Hook ka result | Tuple, tarteeb par bharosa | { invoices, isLoading, error, refresh } |
| Overdue rule | Component mein inline | isOverdue() pure function, test layak |
| Khali count | 0 screen par | Braced guard null lautata hai |
| States | Nested ternary | renderBody() mein seedhe if |
| Error par retry | Nahi | refresh naam se mila |
5. Kaise apply karein?
Split karte waqt private children parent ke folder mein rakhein: InvoiceList/components/InvoiceRow/InvoiceRow.tsx. State utni gehri rakhein jitni mumkin ho. Usay upar sirf tab uthayein jab do siblings ko chahiye.
Component kabhi seedha fetch na kare. Server par service call, client par typed client wala hook.
6. Exceptions aur common mistakes
Kuch rules yahan sakht house opinion hain, aur bohat si teams alag karti hain:
- Ternary aur
&&: zyada tar React codebases inhe aam use karte hain, aur chhotaisOpen ? "Close" : "Open"bilkul parhne layak hai. Yahan ka rule sakht hai kyunki nested ternary jaldi paheli ban jati hai aur&&ka0wala bug aam hai. Render function har state ko naam deta hai. Trade-off: thora zyada code, magar har branch saaf. - Tuple: React ka apna
useStatetuple lautata hai kyunki do generic values hain jinhe aap rename karte ho. Do value wale chhote hooks mein kai teams tuple theek samajhti hain. Teen ya zyada values par named object hamesha behtar hai. - 400 lines: yeh hard limit hai, target nahi. Aksar 150 line par hi split karna behtar hota hai.
Galati: Hook se JSX lautana, jaise renderModal
Why? Hook UI ka faisla karne lagta hai, aur styling ya layout badalna ho to hook kholna parta hai.
Behtar: Hook sirf data aur callbacks de, jaise isOpen aur open. Markup component banaye.
Galati: Component mein useEffect se data fetch aur transform
Why? Loading, error, race conditions aur transform sab ek file mein aate hain aur reuse nahi hota.
Behtar: Next.js mein server component se service call karo. Client par ek hook jo typed client use kare.
Galati: 400 line chhoone par sirf helpers ko neeche ki taraf move karna
Why? File phir bhi ek hi hai aur ek hi component sab kaam kar raha hai.
Behtar: UI ke asal hisse alag child components banao, har ek apni file aur props type ke saath.
7. Pehle aur baad mein kya seekhein?
| Stage | Topics |
|---|---|
| Pehle | Custom Hooks, Conditional Rendering |
| Saath mein | Conditionals Without Ternaries, Separation of Concerns |
| Baad mein | Next.js Practices, Design Tokens & UI Consistency |
Final quick revision
| Check | Haan / Nahi |
|---|---|
| Component 400 lines se kam | Haan |
| Custom hook tuple lautata hai | Nahi |
| Hook JSX lautata hai | Nahi |
Returned JSX mein ternary ya && | Nahi, renderX() |
Rules aur calculations src/lib mein | Haan |
Component mein fetch | Nahi |