React ยท Chapter 37 of 42

Best Practices

Writing maintainable React code involves consistent conventions: keep components small and focused, colocate related logic, name things clearly, and avoid unnecessary complexity.

Following established patterns (hooks rules, proper key usage, lifting state appropriately) helps your codebase scale as your team and app grow.

Component design

Keep components small and single-purpose. Extract reusable logic into custom hooks. Prefer composition over deeply nested prop drilling.

Code quality tools

Use ESLint (with the react-hooks plugin) and Prettier for consistency, and write tests for critical behavior using React Testing Library.

Example 1 (jsx)
// Good: small, focused, descriptive component
function PriceTag({ amount, currency = "USD" }) {
  return (
    <span className="price-tag">
      {new Intl.NumberFormat("en-US", { style: "currency", currency }).format(amount)}
    </span>
  );
}
Output
$19.99

A small, well-named, single-purpose component with a sensible default prop.

Key points

  • Keep components small, focused, and clearly named.
  • Extract reusable logic into custom hooks.
  • Use ESLint and Prettier to enforce consistent, error-free code.
  • Write tests for critical user-facing behavior.
๐Ÿ’ก Note: Good React code reads like the UI it describes โ€” clear, composable, and predictable.

๐Ÿ“ Quick Quiz

1. What should you extract reusable stateful logic into?

2. What plugin helps enforce correct hook usage?

3. What should component design generally favor?