Install any skill in seconds. Free to start, no credit card required.
Get Started Free →React 18/19 patterns including hooks discipline, server/client component boundaries, Suspense + error boundaries, form actions, data fetching, state management decision trees, and accessibility-first composition. Use when writing or reviewing React components.
.claude/skills/affaan-m-react-patterns/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-01 | ✗→✓ | ▲ Improved | 153% | 0% |
| case-03 | ✓→✓ | = Same ✓ | 115% | 0% |
| case-04 | ✓→✓ | = Same ✓ | 95% | 0% |
| case-05 | ✓→✓ | = Same ✓ | 117% | 0% |
| case-06 | ✓→✓ | = Same ✓ | 85% | 0% |
Idiomatic React 18/19 patterns for building robust, accessible, performant component trees.
forwardRef/useEffect-heavy codetsx// Good: derive during render function Cart({ items }: { items: CartItem[] }) { const total = items.reduce((sum, i) => sum + i.price * i.qty, 0); return <span>{formatMoney(total)}</span>; } // Bad: derived state stored separately function Cart({ items }: { items: CartItem[] }) { const [total, setTotal] = useState(0); useEffect(() => { setTotal(items.reduce((sum, i) => sum + i.price * i.qty, 0)); }, [items]); return <span>{formatMoney(total)}</span>; }
Derived state in useEffect adds a render cycle, can desync, and obscures the data flow.
Effects, mutations, network calls, and subscriptions live in event handlers or useEffect — never in the render body.
React has no inheritance model for components. Compose with children, render props, or component props.
See rules/react/hooks.md for the full ruleset. Highlights:
setX(prev => prev + 1)) when new state depends on olduseMemo/useCallback only when a profiler or a dependency chain proves it mattersUsed by one component?
-> useState inside it
Used by parent + a few descendants?
-> lift to nearest common ancestor
Used across distant branches AND low-frequency reads (theme, auth, locale)?
-> React Context
High-frequency updates shared across the tree?
-> external store (Zustand, Jotai, Redux Toolkit)
Derived from a server?
-> server-state library (TanStack Query, SWR, RSC fetch)Most pages do not need context or a global store. Resist abstraction until duplicated lifting becomes painful.
tsx// Server Component - default, async, never ships JS for itself export default async function ProductPage({ params }: { params: { id: string } }) { const product = await db.product.findUnique({ where: { id: params.id } }); if (!product) notFound(); return <ProductView product={product} />; } // Client Component - opt in with "use client" "use client"; export function AddToCartButton({ productId }: { productId: string }) { const [pending, startTransition] = useTransition(); return ( <button disabled={pending} onClick={() => startTransition(() => addToCart(productId))} > {pending ? "Adding..." : "Add to cart"} </button> ); }
Boundaries:
children<form action={...}> or imperatively from event handlersimport a Server Component from a Client Component file — compose them via children insteadtsx<ErrorBoundary fallback={<ErrorView />}> <Suspense fallback={<UserSkeleton />}> <UserDetail id={id} /> </Suspense> </ErrorBoundary>
react-error-boundary for a hook-friendly wrapper> React 19+: This example uses useActionState. For React 18, use useFormState from react-dom instead.
tsx"use client"; import { useActionState } from "react"; const initial = { error: null as string | null }; async function updateUserAction(_prev: typeof initial, formData: FormData) { "use server"; const parsed = UserSchema.safeParse(Object.fromEntries(formData)); if (!parsed.success) return { error: "Invalid input" }; await db.user.update({ where: { id: parsed.data.id }, data: parsed.data }); return { error: null }; } export function UserForm() { const [state, formAction, pending] = useActionState(updateUserAction, initial); return ( <form action={formAction}> <input name="name" required /> <button type="submit" disabled={pending}>Save</button> {state.error && <p role="alert">{state.error}</p>} </form> ); }
Use controlled when the value drives other UI, formats on every keystroke, or implements real-time validation.
For multi-step forms, dynamic field arrays, or cross-field validation: use a library (React Hook Form, TanStack Form). Roll-your-own state management for forms past trivial complexity is a maintenance trap.
| Need | Tool | |---|---| | Per-request data in Next.js App Router | RSC await fetch() | | Client-side cache + mutations + invalidation | TanStack Query | | Lightweight client cache + revalidation | SWR | | Real-time subscriptions | Server-Sent Events, WebSockets, or the lib's subscription API | | One-off fire-and-forget | fetch() in an event handler |
Avoid useEffect + fetch for application data — race conditions, no cache, no retry, no Suspense integration.
childrentsx<Layout> <Header /> <Main>{content}</Main> </Layout>
tsx<Page header={<Nav />} sidebar={<Filters />}> <Results /> </Page>
tsx<Tabs defaultValue="profile"> <Tabs.List> <Tabs.Trigger value="profile">Profile</Tabs.Trigger> <Tabs.Trigger value="settings">Settings</Tabs.Trigger> </Tabs.List> <Tabs.Panel value="profile"><Profile /></Tabs.Panel> <Tabs.Panel value="settings"><Settings /></Tabs.Panel> </Tabs>
Useful when the parent needs to pass parameters to the rendered output:
tsx<DataLoader id={id}> {({ data, isLoading }) => isLoading ? <Spinner /> : <UserCard user={data} />} </DataLoader>
Modern alternative: a hook (useData(id)) returning the same shape — usually cleaner.
React.memo Actually HelpsWrap a component in React.memo only when:
React.memo adds an equality check on every render. If props differ on most renders, the check is pure overhead.
themeContext does not re-render auth consumersuseSyncExternalStore for external state libraries — required for safe concurrent renderingkey props (database id, not array index)@tanstack/react-virtual or react-window once visible item count exceeds ~50 with non-trivial rows<button>, <a>, <nav>, <main>) before reaching for role attributes<label htmlFor> or aria-label if visually labeled by an iconaxe in component tests (see skills/react-testing)This skill is router-agnostic. The patterns above work with React Router, TanStack Router, Next.js App Router, Remix Router. Router-specific patterns (loaders, actions, nested layouts) follow the router's documentation — those are framework concerns layered on top of React core.
react-native-patterns skill (not present yet)react-reviewer for code review, react-build-resolver for build/bundler errors/react-review, /react-build, /react-testtsxfunction useDebounce<T>(value: T, delay = 300): T { const [debounced, setDebounced] = useState(value); useEffect(() => { const id = setTimeout(() => setDebounced(value), delay); return () => clearTimeout(id); }, [value, delay]); return debounced; } function SearchBox() { const [query, setQuery] = useState(""); const debounced = useDebounce(query, 300); const { data } = useQuery({ queryKey: ["search", debounced], queryFn: () => searchApi(debounced), enabled: debounced.length > 0, }); return ( <> <input value={query} onChange={(e) => setQuery(e.target.value)} /> <Results items={data ?? []} /> </> ); }
useOptimistictsx"use client"; import { useOptimistic } from "react"; export function MessageList({ messages }: { messages: Message[] }) { const [optimistic, addOptimistic] = useOptimistic( messages, (state, newMessage: Message) => [...state, newMessage], ); async function send(formData: FormData) { const text = String(formData.get("text")); addOptimistic({ id: "pending", text, sender: "me" }); await saveMessage(text); } return ( <> <ul>{optimistic.map((m) => <li key={m.id}>{m.text}</li>)}</ul> <form action={send}> <input name="text" /> <button type="submit">Send</button> </form> </> ); }
tsx// Two contexts: one rarely changes, one frequently const ThemeContext = createContext<Theme>("light"); const NotificationsContext = createContext<Notification[]>([]); // A component that only consumes ThemeContext does NOT re-render when notifications change
| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-03 | pass→pass | 10,668 | 10,081 | -6% | 1 | 1 | 0% | 2,318 | 4,989 | +115% | 0 | 0 | — |
case-01 | fail→pass | 9,891 | 9,444 | -5% | 1 | 1 | 0% | 1,945 | 4,920 | +153% | 0 | 0 | — |
case-02 | fail→fail | 20,446 | 19,545 | -4% | 1 | 1 | 0% | 4,639 | 7,485 | +61% | 0 | 0 | — |
case-04 | pass→pass | 13,292 | 11,627 | -13% | 1 | 1 | 0% | 2,844 | 5,546 | +95% | 0 | 0 | — |
case-05 | pass→pass | 8,864 | 6,331 | -29% | 1 | 1 | 0% | 1,970 | 4,267 | +117% | 0 | 0 | — |
case-06 | pass→pass | 13,389 | 11,838 | -12% | 1 | 1 | 0% | 2,855 | 5,289 | +85% | 0 | 0 | — |
case-07 | pass→pass | 12,378 | 8,077 | -35% | 1 | 1 | 0% | 2,743 | 4,671 | +70% | 0 | 0 | — |
case-08 | pass→pass | 13,981 | 13,624 | -3% | 1 | 1 | 0% | 2,780 | 5,795 | +108% | 0 | 0 | — |
case-09 | pass→pass | 22,688 | 10,288 | -55% | 1 | 1 | 0% | 2,251 | 5,094 | +126% | 0 | 0 | — |
case-10 | pass→pass | 15,166 | 13,377 | -12% | 1 | 1 | 0% | 2,409 | 5,213 | +116% | 0 | 0 | — |
case-11 | pass→pass | 7,081 | 5,819 | -18% | 1 | 1 | 0% | 1,441 | 4,175 | +190% | 0 | 0 | — |
case-12 | pass→pass | 11,854 | 10,133 | -15% | 1 | 1 | 0% | 2,341 | 4,752 | +103% | 0 | 0 | — |
case-21 | fail→fail | 15,390 | 9,632 | -37% | 1 | 1 | 0% | 3,543 | 4,979 | +41% | 0 | 0 | — |
case-13 | pass→pass | 8,480 | 7,997 | -6% | 1 | 1 | 0% | 1,807 | 4,676 | +159% | 0 | 0 | — |
case-14 | pass→pass | 13,365 | 10,191 | -24% | 1 | 1 | 0% | 2,306 | 4,666 | +102% | 0 | 0 | — |
case-15 | fail→fail | 11,994 | 8,469 | -29% | 1 | 1 | 0% | 2,152 | 4,438 | +106% | 0 | 0 | — |
case-16 | pass→pass | 11,041 | 8,600 | -22% | 1 | 1 | 0% | 2,459 | 4,838 | +97% | 0 | 0 | — |
case-22 | fail→fail | 11,998 | 10,659 | -11% | 1 | 1 | 0% | 2,559 | 5,141 | +101% | 0 | 0 | — |
case-17 | pass→pass | 7,785 | 4,426 | -43% | 1 | 1 | 0% | 1,488 | 3,715 | +150% | 0 | 0 | — |
case-18 | pass→pass | 18,070 | 5,978 | -67% | 1 | 1 | 0% | 1,601 | 4,129 | +158% | 0 | 0 | — |
case-19 | pass→pass | 11,117 | 8,926 | -20% | 1 | 1 | 0% | 2,160 | 4,656 | +116% | 0 | 0 | — |
case-20 | fail→fail | 11,446 | 7,176 | -37% | 1 | 1 | 0% | 2,643 | 4,460 | +69% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 22 cases were attempted. The headline lift of +5 percentage points is the difference between those two pass rates over the 22 comparable cases.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
Other measured skills in the registry, with their headline benchmark lift.