Skip to content
Frontend & UIChapter 40 / 57Intermediate6 min read

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 ne const [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 par 0 likh diya, kyunki React 0 ko render karta hai.
  • 1200 line ka Dashboard.tsx koi review nahi kar paata tha, is liye har PR "LGTM" ho kar merge ho jata tha.
Bina rulesRules ke saath
Tuple ki tarteeb ghalat, bug chupke seNamed object, naam se value, tarteeb ka masla nahi
Nested ternary ko parhna pahelirenderBody mein seedhe if aur return
Length aur double ampersand se 0 screen parBraced guard, sirf element ya null
1000 line ka component, review na mumkinChhote child components, apni apni file
Rule component mein, test ke liye renderRule pure function mein, seedha test

3. Kab follow karein?

LevelKitna zarooriKyun
Learning projectLogic hooks mein nikalna aur components chhote rakhna seekheinYeh React ki sab se faidemand aadat hai
Production codeCharon rules lazmi, review mein checkTeam ka code tab hi review hota hai jab chhota aur pehle se andaza layak ho
Senior levelLint rules aur component patterns set karnareact/jsx-no-leaked-render jaise rules review ka bojh kam karte hain

4. Bura vs acha code

Bura: tuple, ternary aur logic andar

tsx
"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:

ts
export type UseInvoicesResult = {
  invoices: Invoice[];
  isLoading: boolean;
  error: string | null;
  refresh: () => Promise<void>;
};

Rule src/lib/invoices/invoices.library.ts mein:

ts
export function isOverdue(invoice: Invoice, now: Date) {
  return !invoice.paidAt && invoice.dueAt < now;
}

Component sirf jorta aur dikhata hai:

tsx
"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?

CheezBuraAcha
Hook ka resultTuple, tarteeb par bharosa{ invoices, isLoading, error, refresh }
Overdue ruleComponent mein inlineisOverdue() pure function, test layak
Khali count0 screen parBraced guard null lautata hai
StatesNested ternaryrenderBody() mein seedhe if
Error par retryNahirefresh naam se mila

5. Kaise apply karein?

Component likhte waqt
State aur data ka kaam custom hook mein
Hook named object lautaye, JSX nahi
Rules aur calculations src/lib ke pure functions mein
Har conditional block ek renderX function
File 400 lines ke qareeb ho to child components alag files mein

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 chhota isOpen ? "Close" : "Open" bilkul parhne layak hai. Yahan ka rule sakht hai kyunki nested ternary jaldi paheli ban jati hai aur && ka 0 wala bug aam hai. Render function har state ko naam deta hai. Trade-off: thora zyada code, magar har branch saaf.
  • Tuple: React ka apna useState tuple 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?

Suggested order
Custom Hooks
Conditionals Without Ternaries
React Component Practices
Next.js Practices

Final quick revision

CheckHaan / Nahi
Component 400 lines se kamHaan
Custom hook tuple lautata haiNahi
Hook JSX lautata haiNahi
Returned JSX mein ternary ya &&Nahi, renderX()
Rules aur calculations src/lib meinHaan
Component mein fetchNahi