React modern

From WrongStack/WrongStack · View interactive version

React modern is an AI agent skill from the GitHub repository WrongStack/WrongStack. React components and hooks: state, effects, re-renders, data fetching, forms, Suspense, Server and Client Components, React 19 APIs. Use this skill when writing, reviewing, or debugging React components and hooks — state, effects, data fetching, forms, Server and Client Components — in whatever Reac

How to use this skill

Skill file

Modern React

Selection card

Overview

Most React bugs come from the same few places: state that should have been

derived, effects used as event handlers, and data fetching that races. Before

applying any pattern, establish the setup — the React version in package.json,

and whether a framework with Server Components (Next.js App Router, React

Router framework mode) is in play or it is a client-only app (Vite, CRA, Expo).

Several "modern" rules only apply to one of the two.

Rules

  1. Pre-flight: Inspect repo & live registry versions first. Check package.json

for the exact React version ("react", "react-dom") and framework setup. Query

registry.npmjs.org/react/latest or online docs to verify current React 19 stable

capabilities (such as useActionState, useOptimistic, use(), and ref-as-prop without forwardRef).

If the repo is on React 18, identify upgrade steps before applying React 19-only hooks.

  1. Match the setup. Server Components, 'use client', and server actions exist

only in RSC frameworks. In a client-only app, fetch through the project's data

layer (TanStack Query, SWR, a loader) instead.

  1. Derive, don't sync. If a value can be computed from props or state during

render, compute it; don't mirror it into state with an effect.

  1. Effects are for synchronizing with external systems (subscriptions, DOM

APIs, timers, non-React widgets). User-caused changes belong in event

handlers.

  1. Every effect that subscribes or starts work returns a cleanup, and every

async effect guards against stale responses (abort or ignore flag).

  1. Never mutate state or props; update with new objects and arrays.
  2. Keys are stable identities from the data — never array indexes for lists

that reorder, insert, or delete.

  1. Memoize after measuring; inspect compiler coverage before adding manual memoization.

useMemo/useCallback exist for expensive work and referential stability

a child or effect actually depends on.

  1. Follow the framework's file conventions over style preferences — Next.js

page.tsx, layout.tsx, and route files require a default export even when

the codebase otherwise prefers named exports.

  1. Accessible by default: semantic elements, labels on inputs, keyboard

operability, focus management for dialogs.

Patterns

// Derived value — no state, no effect.
function Cart({ items }: { items: CartItem[] }) {
  const total = items.reduce((sum, item) => sum + item.price * item.qty, 0);
  return <p>Total: {formatMoney(total)}</p>;
}

// Effect that syncs with an external system, with cleanup.
function useOnlineStatus(): boolean {
  const [online, setOnline] = useState(() => navigator.onLine);
  useEffect(() => {
    const update = () => setOnline(navigator.onLine);
    window.addEventListener('online', update);
    window.addEventListener('offline', update);
    return () => {
      window.removeEventListener('online', update);
      window.removeEventListener('offline', update);
    };
  }, []);
  return online;
}

// Client-only fetch without a data library: abort stale requests.
function useUser(id: string) {
  const [state, setState] = useState<{ user?: User; error?: Error }>({});
  useEffect(() => {
    const controller = new AbortController();
    fetchUser(id, controller.signal)
      .then((user) => setState({ user }))
      .catch((error: Error) => {
        if (error.name !== 'AbortError') setState({ error });
      });
    return () => controller.abort();
  }, [id]);
  return state;
}
// RSC frameworks only: server data in a Server Component, interactivity in a leaf.
export default async function ProfilePage({ params }: { params: Promise<{ id: string }> }) {
  const { id } = await params;
  const user = await getUser(id); // runs on the server
  return <ProfileHeader user={user} />;
}

// React 19 form action with pending and error state.
'use client';
function RenameForm({ rename }: { rename: (prev: State, data: FormData) => Promise<State> }) {
  const [state, action, pending] = useActionState(rename, { error: null });
  return (
    <form action={action}>
      <label htmlFor="name">Name</label>
      <input id="name" name="name" required />
      <button type="submit" disabled={pending}>Save</button>
      {state.error ? <p role="alert">{state.error}</p> : null}
    </form>
  );
}

React 19 notes

forwardRef.

(passed from a Server Component, or cached) — a promise created during render

is recreated on every render.

hand-rolled pending and error state for forms.

memoized automatically: skip new useMemo/useCallback, and treat its

bail-outs and the hooks lint rules as signals of rule-breaking code.

prefer it to conditional unmounting for tabs and back navigation.

Debugging re-renders and stale values

| Symptom | Usual cause | Fix |

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

| Effect runs in a loop | Object or function dependency recreated each render | Move it inside the effect, or derive a primitive dependency |

| Handler sees an old value | Closure captured stale state | Functional update setX((prev) => …), or read the latest value inside the effect |

| Input loses focus while typing | Component defined inside another component, or unstable key | Hoist the component; stable keys |

| List items swap state | Index keys on a reordering list | Keys from item identity |

| Hydration mismatch | Rendering time, randomness, or browser-only values on the server | Render those after mount, or pass them from the server |

Anti-patterns

everything to the browser.

Before returning

Current React and async semantics

Target React/react-dom 19.3.1, checked from npm on 2026-11-02; upgrade the pair

with the framework and type packages after checking peer requirements.

Keep rendering pure, make cleanup work under Strict Mode and distinguish

optimistic state from persisted success. Server Action authorization belongs

server-side even when useActionState provides UI feedback.

useEffectEvent is for non-reactive logic called from Effects; it is not a way

to hide reactive dependencies or create an ordinary click handler.

Check its current semantics.

Skills in scope

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.