React Component Practices
React best practices for components and hooks. A 400 line limit, hooks that return a named state object, render functions for conditions and logic outside the component.
On this page
1. What is this practice?
A component's job is to display. Handling state is the hook's job, and rules (what is overdue, what the total is) belong to pure functions in src/lib. When a component only builds markup from props and hook values, it stays small, readable and safe to change.
2. Why does it matter?
Three real bugs these rules stop:
- A hook returned
[invoices, isLoading, error]. In one file someone wroteconst [invoices, error, isLoading]. Nowif (isLoading)ran on the error string, and when an error happened the skeleton just kept spinning. {items.length && <List />}printed0on screen for an empty list, because React renders0.- A 1200 line
Dashboard.tsxcould not be reviewed by anyone, so every PR got an "LGTM" and was merged.
| Without rules | With rules |
|---|---|
| Wrong tuple order, a silent bug | A named object, value by name, no order problem |
| A nested ternary is a puzzle to read | Plain if and return inside renderBody |
| Length and double ampersand put 0 on screen | A braced guard, returns only an element or null |
| A 1000 line component, review is impossible | Small child components, each in its own file |
| The rule lives in the component, rendering is needed to test it | The rule is in a pure function, tested directly |
3. When to follow it
| Level | How important | Why |
|---|---|---|
| Learning project | Learn to move logic into hooks and keep components small | This is the most useful habit in React |
| Production code | All four rules required, checked in review | A team's code only gets reviewed when it is small and predictable |
| Senior level | Set up lint rules and component patterns | Rules like react/jsx-no-leaked-render reduce the review burden |
4. Bad vs good code
Bad: tuple, ternary and logic inside
"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>
);
}Good: named state, render functions, logic outside
The hook's result type goes in use-invoices.types.ts:
export type UseInvoicesResult = {
invoices: Invoice[];
isLoading: boolean;
error: string | null;
refresh: () => Promise<void>;
};The rule goes in src/lib/invoices/invoices.library.ts:
export function isOverdue(invoice: Invoice, now: Date) {
return !invoice.paidAt && invoice.dueAt < now;
}The component only connects and displays:
"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>
);
}What is the difference?
| Thing | Bad | Good |
|---|---|---|
| Hook result | A tuple, relying on order | { invoices, isLoading, error, refresh } |
| Overdue rule | Inline in the component | An isOverdue() pure function, easy to test |
| Empty count | 0 on screen | The braced guard returns null |
| States | Nested ternary | Plain if inside renderBody() |
| Retry on error | None | refresh comes by name |
5. How to apply it
When you split, keep private children in the parent's folder: InvoiceList/components/InvoiceRow/InvoiceRow.tsx. Keep state as deep as possible. Lift it up only when two siblings need it.
A component should never call fetch directly. On the server, call a service. On the client, use a hook that uses the typed client.
6. Exceptions and common mistakes
Some of these rules are strict house opinions here, and many teams do things differently:
- Ternary and
&&: most React codebases use them freely, and a shortisOpen ? "Close" : "Open"is perfectly readable. The rule is strict here because a nested ternary quickly becomes a puzzle and the0bug with&&is common. A render function gives every state a name. The trade-off: a bit more code, but every branch is clear. - Tuple: React's own
useStatereturns a tuple because it has two generic values that you rename. Many teams find a tuple fine for small hooks with two values. With three or more values, a named object is always better. - 400 lines: this is a hard limit, not a target. Often splitting at 150 lines is better.
Mistake: Returning JSX from a hook, like renderModal
Why? The hook starts deciding the UI, and you have to open the hook whenever styling or layout changes.
Better: The hook gives only data and callbacks, like isOpen and open. The component builds the markup.
Mistake: Fetching and transforming data with useEffect inside a component
Why? Loading, error, race conditions and transforms all land in one file and nothing is reusable.
Better: In Next.js, call the service from a server component. On the client, use one hook that uses the typed client.
Mistake: Only moving helpers to the bottom when you hit 400 lines
Why? The file is still one file and one component is still doing all the work.
Better: Make the real parts of the UI separate child components, each in its own file with its own props type.
7. What to learn before and after
| Stage | Topics |
|---|---|
| Before | Custom Hooks, Conditional Rendering |
| Alongside | Conditionals Without Ternaries, Separation of Concerns |
| After | Next.js Practices, Design Tokens & UI Consistency |
Final quick revision
| Check | Yes / No |
|---|---|
| Component under 400 lines | Yes |
| Custom hook returns a tuple | No |
| Hook returns JSX | No |
Ternary or && in returned JSX | No, use renderX() |
Rules and calculations in src/lib | Yes |
fetch in a component | No |