ReactJS Coding Standards
Enterprise-level React standards for teams building production frontend applications. These rules apply to all React and TypeScript code in shared repositories.
Toolchain and Setup
All production React projects should use:
- TypeScript - mandatory for type safety
- ESLint with recommended React and TypeScript rules
- Prettier for consistent formatting
- Vitest or Jest for unit tests
- React Testing Library for component tests
Enforce ESLint and Prettier in CI so no non-conforming code reaches main.
Naming Conventions
// Component files use PascalCase
OrderSummary.tsx
PaymentForm.tsx
// Component names match the file name
export function OrderSummary() { }
export function PaymentForm() { }
// Hooks are prefixed with use
function useOrderStatus(orderId: string) { }
function usePaymentForm() { }
// Event handlers are prefixed with handle
function handleSubmit(event: React.FormEvent) { }
function handleQuantityChange(value: number) { }
// Boolean props and state start with is, has, or should
const isLoading = true;
const hasError = false;
const shouldShowSummary = items.length > 0;
// Types and interfaces use PascalCase
interface OrderSummaryProps {
orderId: string;
customerId: string;
}
type OrderStatus = "pending" | "confirmed" | "shipped" | "cancelled";
Folder and Project Structure
src/
features/
orders/
components/
OrderSummary.tsx
OrderSummary.test.tsx
OrderLineItem.tsx
hooks/
useOrderStatus.ts
useOrderList.ts
api/
ordersApi.ts
types/
order.types.ts
index.ts
payments/
components/
hooks/
api/
types/
index.ts
shared/
components/
Button/
Button.tsx
Button.test.tsx
index.ts
Modal/
Table/
hooks/
usePagination.ts
useDebounce.ts
utils/
formatCurrency.ts
formatDate.ts
types/
api.types.ts
app/
store.ts
routes.tsx
App.tsx
main.tsx
Rule of thumb:
- feature folders own everything for that feature
- shared folder is for truly reusable components used across features
- API calls are isolated in feature-level api files
- components do not call APIs directly - hooks do
Component Design
Prefer small, focused, composable components.
// Single responsibility - this component renders only the summary card
interface OrderSummaryProps {
orderId: string;
customerName: string;
totalAmount: number;
currency: string;
status: OrderStatus;
}
export function OrderSummary({
orderId,
customerName,
totalAmount,
currency,
status,
}: OrderSummaryProps) {
return (
<div className="order-summary">
<h2>Order {orderId}</h2>
<p>{customerName}</p>
<p>{formatCurrency(totalAmount, currency)}</p>
<OrderStatusBadge status={status} />
</div>
);
}
Guidelines:
- avoid components that fetch data, render UI, and manage state all at once
- extract rendering logic into smaller child components
- extract data-fetching into custom hooks
Custom Hooks
Hooks encapsulate data fetching and stateful logic, keeping components clean.
// useOrderStatus.ts
import { useQuery } from "@tanstack/react-query";
import { fetchOrderStatus } from "../api/ordersApi";
interface UseOrderStatusResult {
status: OrderStatus | undefined;
isLoading: boolean;
isError: boolean;
error: Error | null;
}
export function useOrderStatus(orderId: string): UseOrderStatusResult {
const { data, isLoading, isError, error } = useQuery({
queryKey: ["order-status", orderId],
queryFn: () => fetchOrderStatus(orderId),
enabled: Boolean(orderId),
});
return { status: data, isLoading, isError, error };
}
State Management
Use the right tool for the right scope.
| Scope | Recommended approach |
|---|---|
| Local UI state | useState, useReducer |
| Server state | React Query or SWR |
| Cross-feature global state | Zustand or Redux Toolkit |
| Form state | React Hook Form |
Avoid putting server data into global state. React Query caches server data naturally.
API Layer
Isolate all HTTP calls in a dedicated API module for each feature. Never call fetch or axios directly inside components or hooks without an abstraction.
// ordersApi.ts
import { apiClient } from "@/shared/utils/apiClient";
import type { Order, OrderStatus } from "../types/order.types";
export async function fetchOrderStatus(orderId: string): Promise<OrderStatus> {
const response = await apiClient.get<OrderStatus>(`/orders/${orderId}/status`);
return response.data;
}
export async function createOrder(payload: CreateOrderPayload): Promise<Order> {
const response = await apiClient.post<Order>("/orders", payload);
return response.data;
}
Error Handling
Use error boundaries for rendering failures. Handle async errors in hooks with try/catch or React Query's error state.
// ErrorBoundary wraps a feature section
<ErrorBoundary fallback={<OrdersErrorFallback />}>
<OrdersPage />
</ErrorBoundary>
// Async error in a hook
const { data, isError, error } = useOrderStatus(orderId);
if (isError) {
return <AlertBanner message="Failed to load order status." />;
}
Testing Expectations
Test behaviour from the user's perspective using React Testing Library.
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { OrderSummary } from "./OrderSummary";
describe("OrderSummary", () => {
it("renders customer name and formatted total", () => {
render(
<OrderSummary
orderId="ORD-001"
customerName="Acme Corp"
totalAmount={299.99}
currency="GBP"
status="confirmed"
/>
);
expect(screen.getByText("Acme Corp")).toBeInTheDocument();
expect(screen.getByText("£299.99")).toBeInTheDocument();
});
});
Common Pitfalls
- direct DOM manipulation instead of React state
- deeply nested prop drilling instead of context or state management
- importing from another feature's internal files instead of its index.ts
- putting business logic inside JSX
- missing keys on list items
- using any as a type shortcut
Key Takeaways
- TypeScript and ESLint are non-negotiable in enterprise React projects
- components are for rendering; hooks are for data and logic
- feature-based folder structure keeps large applications navigable
- test behaviour, not implementation
- server state belongs in React Query, not Redux