React TypeScript
Patterns for building type-safe React 19.2 applications with TypeScript 5.9. React Compiler handles memoization automatically - write plain components, let the tooling optimize.
Critical Rules
No forwardRef - ref Is a Prop Now
// WRONG - deprecated pattern
const Input = forwardRef<HTMLInputElement, InputProps>((props, ref) => (
<input ref={ref} {...props} />
))
// CORRECT - React 19: ref is a regular prop
function Input({ ref, ...props }: React.ComponentProps<"input">) {
return <input ref={ref} {...props} />
}
No Manual Memoization with React Compiler
// WRONG - unnecessary with React Compiler
const MemoizedList = memo(function List({ items }: { items: Item[] }) {
const sorted = useMemo(() => items.toSorted(compare), [items])
const handleClick = useCallback((id: string) => onSelect(id), [onSelect])
return sorted.map(item => <Row key={item.id} onClick={() => handleClick(item.id)} />)
})
// CORRECT - React Compiler auto-memoizes all of this
function List({ items, onSelect }: { items: Item[]; onSelect: (id: string) => void }) {
const sorted = items.toSorted(compare)
return sorted.map(item => <Row key={item.id} onClick={() => onSelect(item.id)} />)
}
Use React.ComponentProps<> for Element Props
// WRONG - manual HTML attribute typing
interface ButtonProps {
onClick?: (e: MouseEvent<HTMLButtonElement>) => void
disabled?: boolean
children: React.ReactNode
className?: string
}
// CORRECT - extend native element props
type ButtonProps = React.ComponentProps<"button"> & {
variant?: "primary" | "ghost"
}
Type State Discriminated Unions, Not Booleans
// WRONG - impossible states possible
interface RequestState { isLoading: boolean; error: string | null; data: User | null }
// CORRECT - discriminated union prevents impossible states
type RequestState =
| { status: "idle" }
| { status: "loading" }
| { status: "error"; error: string }
| { status: "success"; data: User }
Use satisfies for Type-Safe Literals
// WRONG - widens to Record<string, Route>
const routes: Record<string, Route> = { home: { path: "/" }, about: { path: "/about" } }
// CORRECT - preserves literal keys while checking shape
const routes = {
home: { path: "/" },
about: { path: "/about" },
} satisfies Record<string, Route>
routes.home // typed, autocomplete works
Context Must Have Strict Defaults or Throw
// WRONG - null default with no guard
const AuthContext = createContext<AuthState | null>(null)
// consumers must null-check every time
// CORRECT - factory hook that throws on missing provider
const AuthContext = createContext<AuthState | null>(null)
function useAuth(): AuthState {
const ctx = use(AuthContext)
if (ctx === null) throw new Error("useAuth must be used within AuthProvider")
return ctx
}
Prefer use() over useContext()
// OLD pattern
function Header() {
const theme = useContext(ThemeContext) // cannot use after early return
if (!isVisible) return null
return <h1 style={{ color: theme.color }}>Title</h1>
}
// CORRECT - React 19: use() works after early returns
function Header({ isVisible }: { isVisible: boolean }) {
if (!isVisible) return null
const theme = use(ThemeContext) // works here - use() is not bound by hook rules
return <h1 style={{ color: theme.color }}>Title</h1>
}
React 19 Patterns
Component Authoring
Plain functions with data-slot for styling hooks. No forwardRef, no FC:
type CardProps = React.ComponentProps<"div"> & {
variant?: "elevated" | "outlined"
}
function Card({ variant = "outlined", className, ...props }: CardProps) {
return (
<div
data-slot="card"
data-variant={variant}
className={cn("rounded-xl border bg-card", className)}
{...props}
/>
)
}
function CardTitle({ className, ...props }: React.ComponentProps<"h3">) {
return <h3 data-slot="card-title" className={cn("font-semibold", className)} {...props} />
}
Actions and useTransition
Async functions in transitions handle pending state, errors, and form resets automatically:
function UpdateProfile({ userId }: { userId: string }) {
const [error, submitAction, isPending] = useActionState(
async (_prev: string | null, formData: FormData) => {
const result = await updateProfile(userId, formData)
if (result.error) return result.error
redirect("/profile")
return null
},
null
)
return (
<form action={submitAction}>
<input type="text" name="displayName" required />
<button type="submit" disabled={isPending}>
{isPending ? "Saving..." : "Save"}
</button>
{error && <p className="text-destructive">{error}</p>}
</form>
)
}
useTransition for non-form Actions:
function DeleteButton({ onDelete }: { onDelete: () => Promise<void> }) {
const [isPending, startTransition] = useTransition()
return (
<button
disabled={isPending}
onClick={() => startTransition(async () => { await onDelete() })}
>
{isPending ? "Deleting..." : "Delete"}
</button>
)
}
useOptimistic for instant feedback:
function LikeButton({ likes, onLike }: { likes: number; onLike: () => Promise<void> }) {
const [optimisticLikes, addOptimisticLike] = useOptimistic(likes, (prev) => prev + 1)
const handleLike = async () => {
addOptimisticLike(null)
await onLike()
}
return (
<form action={handleLike}>
<button type="submit">{optimisticLikes} Likes</button>
</form>
)
}
use() Hook
Read promises and context in render. Works conditionally, after early returns:
// Reading a promise - suspends until resolved
function Comments({ commentsPromise }: { commentsPromise: Promise<Comment[]> }) {
const comments = use(commentsPromise)
return (
<ul>
{comments.map(c => <li key={c.id}>{c.text}</li>)}
</ul>
)
}
// Parent gets promise from loader/cache, NOT created during render
function PostPage({ commentsPromise }: { commentsPromise: Promise<Comment[]> }) {
return (
<Suspense fallback={<Skeleton />}>
<Comments commentsPromise={commentsPromise} />
</Suspense>
)
}
// Reading context conditionally
function AdminPanel({ user }: { user: User | null }) {
if (!user) return <LoginPrompt />
const permissions = use(PermissionsContext) // legal - use() works after early return
if (!permissions.isAdmin) return <Forbidden />
return <Dashboard user={user} permissions={permissions} />
}
Important: use() does not support promises created during render. Pass promises from loaders, server functions, or cached sources.
Activity Component (React 19.2)
Preserve state of hidden UI. Hidden children keep their state and DOM but unmount effects:
import { Activity, useState } from "react"
function TabLayout({ tabs }: { tabs: TabConfig[] }) {
const [activeTab, setActiveTab] = useState(tabs[0].id)
return (
<div>
<nav>
{tabs.map(tab => (
<button key={tab.id} onClick={() => setActiveTab(tab.id)}>
{tab.label}
</button>
))}
</nav>
{tabs.map(tab => (
<Activity key={tab.id} mode={activeTab === tab.id ? "visible" : "hidden"}>
<tab.component />
</Activity>
))}
</div>
)
}
Key behaviors:
visible- renders normally, effects mountedhidden- hides viadisplay: none, effects cleaned up, state preserved, updates deferred- Pre-rendering:
<Activity mode="hidden">renders children at low priority for faster future reveals - DOM side effects (video, audio) persist when hidden - add
useLayoutEffectcleanup
useEffectEvent (React 19.2)
Extract non-reactive logic from effects. The event function always sees latest props/state without triggering effect re-runs:
function ChatRoom({ roomId, theme }: { roomId: string; theme: strin