React

From ericrisco/rsc-harness · View interactive version

React is an AI agent skill from the GitHub repository ericrisco/rsc-harness. Use when building or reviewing a client-side React SPA bundled by Vite (React 19.2): components, where state lives, hooks, server data fetching, client routing, re-render and effect bugs. NOT App Router / server components / SSR (that is `nextjs`); NOT native screens (that is `react-native`).

How to use this skill

Skill file

React + Vite SPA — Components, State, Data Fetching (React 19.2, 2026)

Hand-off — picking dependencies. Which library to reach for (UI primitives, command menus,

toasts, OTP inputs, charts, virtualization, drag and drop, state, styling, theming) is

this skill's call: reuse what the project already depends on, then prefer a maintained, accessible library

over hand-rolling a component. How the screen should behave for people is ../ux/SKILL.md's. The look of a page

or screen, and its motion layer, is ../design/SKILL.md's.

Build or review a fast, typed, correctly-architected client-side React single-page app bundled by Vite. No server runtime, no RSC tree, no framework router. Server-rendered React (App Router, server actions, SSR/SSG) is not this skill — that is ../nextjs/SKILL.md.

SDD gate — before writing feature code. If this skill fired on a new, non-trivial feature or behaviour change and there is no approved spec + plan under 02-DOCS/wiki/sdd/, stop and hand off to ../specify/SKILL.md: it runs brainstorm → spec → plan → tasks, then routes back here once the plan is approved. Build directly only for a genuinely one-line / low-risk change. Method: ../asdd/SKILL.md.

First: confirm it's a Vite SPA, not a framework

Read package.json before giving any advice — it stops you applying SSR/RSC patterns to a client SPA.

| Signal in package.json | Read it as | Where |

| ------------------------------------- | --------------------- | ---------------------- |

| vite + react, no next | Vite SPA | here |

| next | metaframework / RSC | ../nextjs/SKILL.md |

| @react-router/dev | RR framework mode/SSR (Remix successor) | ../nextjs/SKILL.md-shape |

| react-router only | RR library mode | here (Routing) |

| expo / react-native | native | ../react-native/SKILL.md |

Then pick the data layer (TanStack Query, always) and the router (React Router v7 library mode or TanStack Router).

Adjacent jobs route out: the type system itself (generics, discriminated unions, tsconfig theory) with no React shape → ../typescript/SKILL.md; visual design, tokens, spacing, button states → ../design/SKILL.md; writing the Vitest/RTL or E2E suite as the task → ../testing-web/SKILL.md; hosting the built dist/ → ../deployment/SKILL.md.

Component & state architecture

| Where does this state live? | Trigger |

| -------------------------------------- | ------------------------------------------------------------- |

| Local useState/useReducer | only one component cares |

| Lifted to nearest common parent | a few siblings share it |

| URL search params | it should be shareable / bookmarkable / survive reload |

| Context | wide read, low write frequency (theme, auth user) |

| External store (Zustand) + selectors | wide read, high write frequency, or deep prop-drilling |

| TanStack Query cache | it's server data (anything fetched) |

// Bad: syncing a derived value into state with an effect → stale + extra render
const [fullName, setFullName] = useState("");
useEffect(() => { setFullName(`${first} ${last}`); }, [first, last]);

// Good: derive in render
const fullName = `${first} ${last}`;

Hooks discipline (React 19.2)

// Effect re-subscribes only when roomId changes, but still logs the latest theme.
const onConnected = useEffectEvent(() => log("connected", theme));
useEffect(() => {
  const c = connect(roomId);
  c.on("open", onConnected);
  return () => c.close();
}, [roomId]); // theme is NOT a dependency
// Bad: new promise each render → infinite suspense loop
function Profile({ id }: { id: string }) {
  const user = use(fetch(`/api/users/${id}`).then(r => r.json())); // ❌
}
// Good: the promise is owned by a cache (useSuspenseQuery, below)

Data fetching — TanStack Query, not useEffect (the headline rule)

Fetching server data in useEffect gives you waterfalls, race conditions, no caching, no dedupe, and double-fires under Strict Mode. Use TanStack Query v5 for all server state.

// Bad: the effect-as-fetch anti-pattern — races, no cache, refetches on every mount
const [user, setUser] = useState<User | null>(null);
useEffect(() => {
  fetch(`/api/users/${id}`).then(r => r.json()).then(setUser); // ❌ stale id, race, no error/loading
}, [id]);

// Good
const { data: user, isPending, isError } = useQuery({
  queryKey: ["user", id],          // identity + cache key + dedupe
  queryFn: () => getUser(id),       // typed fetcher
  staleTime: 60_000,                // 1 min "fresh" → no needless refetch
});
const qc = useQueryClient();
const remove = useMutation({
  mutationFn: deleteUser,
  onSuccess: () => qc.invalidateQueries({ queryKey: ["users"] }),
});

Routing (client-side)

React Router v7 — library mode is the default SPA router:

const router = createBrowserRouter([
  { path: "/", element: <Layout />, children: [
    { index: true, element: <Home /> },
    { path: "users/:id", lazy: () => import("./routes/user") }, // code-split route
  ]},
]);
// <RouterProvider router={router} />

Performance

Measure first with the React DevTools Profiler — guessing wastes effort. Then:

TypeScript + Vite project setup

Anti-patterns → STOP

| Anti-pattern | Reality | Do instead |

| ----------------------------------------------------- | ----------------------------------------------------------------------- | --------------------------------------------------- |

| "Fetch in useEffect, it's simpler" | Waterfalls, races, double-fire, no cache/dedupe | useQuery / useSuspenseQuery |

| "Create the promise inline for use()" | New promise every render → suspends forever | Promise owned by a cache (TanStack Query) |

| "Index as key, the list looks fine" | On reorder/delete, state sticks to the wrong row, list flickers | Stable id as key |

| "Put it in context so anyone can read it" | High-write context re-renders the whole consumer tree | Zustand + narrow selector |

| "Sync the prop into state with an effect" | Duplicated, stale state + an extra render | Derive in render, or key to reset a subtree |

| "useMemo/useCallback everywhere for speed" | Noise; with React Compiler it's redundant | Measure first; let the compiler memoize |

| "VITE_API_SECRET is fine, it's an env var" | It ships in the browser bundle — fully public | Proxy the secret through a backend |

| "useEffect to compute a derived value" | Extra render + a stale window | Recompute in render |

Verify

scripts/verify.sh runs from the project root: ESLint → tsc --noEmit → Vitest → vite build, in that order. Each tool is detected and skipped with a warning (never a failure) if absent. The final vite build writes dist/; the lint/type/test steps are read-only. No installs, no network mutations, safe to re-run. It exits non-zero only on a real tool failure, and exits 0 on a clean/empty target.

Project grounding (02-DOCS)

If the workspace has 02-DOCS/, record stack-specific React conventions (chosen router, store, query defaults) in 02-DOCS/wiki/stack/react.md and index it from CLAUDE.md. Recorded, not gated — skip silently if there is no 02-DOCS/.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.