React Style Guide

Welcome to the React Style Guide.

This guide is intended for mid-level React developers with a few years of experience who are able to get work done, but find themselves struggling to write code that is easy to maintain and scale.

Frameworks

This guide is intended for mid-level React developers with a few years of experience who are able to get work done, but find themselves struggling to write code that is easy to maintain and scale. This guide is not intended for beginners.

Libraries

This guide is intended for mid-level React developers with a few years of experience who are able to get work done, but find themselves struggling to write code that is easy to maintain and scale. This guide is not intended for beginners.

Best Practices

1. State Scope

Hold each piece of React state in the lowest common ancestor that consumes it; lift only when multiple siblings need the same data.

2. State Derivation

Do not store values that can be derived from props or other state; compute them on demand or memoise.

3. Prop Immutability

Treat props as immutable inputs—never mutate them or depend on external mutation.

4. Component Single Responsibility

Keep each component focused on one concern (UI, state management, or orchestration—never all three).

5. Component Functional

Use function components and Hooks exclusively; avoid class components in new code.

6. Pure Render

Write render logic as a pure function of props and state; never trigger side‑effects during render.

7. Hooks Extraction

Factor reusable logic into custom Hooks; expose plain values and callbacks, not JSX.

8. Effect Isolation

Use `useEffect` (or `useLayoutEffect` only when layout‑critical) solely for side‑effects; keep calculations inside render or `useMemo`.

9. Data Fetching Layering

Fetch remote data at the route or page level (or in a dedicated data layer) and pass it downward; avoid fetching deep in leaf widgets.

10. Context Minimal

Introduce React Context only for truly global, read‑heavy data; never for high‑frequency changing state.

11. Code Splitting

Wrap non‑critical modules in `React.lazy` + `Suspense`; prefer route‑level boundaries.

12. Memoization On Demand

Apply `React.memo`, `useMemo`, and `useCallback` only when profiling identifies a real performance issue.

13. Type Safety

Write components in TypeScript with `strict` mode on; export precise prop and state types.

14. Error Boundaries

Place Error Boundaries at app shell and around separately‑loaded chunks; log and gracefully degrade UI.

15. Accessibility First

Default to semantic HTML elements, ARIA roles only when needed, and keyboard operability for all interactive components.

16. Test User Behavior

Write tests at the behavior level using React Testing Library; avoid shallow rendering.

17. File Structure Feature First

Organise files by feature/domain (not by type), grouping components, hooks, tests, and styles together.

18. Styles Colocated

Co‑locate styling (CSS Modules, CSS‑in‑JS, or Tailwind classes) with components; avoid global cascade leakage.

19. Naming Consistent

Use PascalCase for components, camelCase for hooks and variables, and kebab‑case for filenames.