Loading...
Loading...
React 19 patterns with React Compiler. Automatic optimization, Server Components, use() hook. Trigger: When building React components, using hooks, working with forms, or server/client components.
npx skill4agent add fearovex/claude-config react-19// ✅ React Compiler optimizes this automatically
function ExpensiveComponent({ data }: { data: number[] }) {
const result = data.reduce((acc, n) => acc + n, 0); // Compiler memoizes it
return <div>{result}</div>;
}
// ❌ Unnecessary in React 19 with Compiler enabled
function ExpensiveComponent({ data }: { data: number[] }) {
const result = useMemo(() => data.reduce((acc, n) => acc + n, 0), [data]);
return <div>{result}</div>;
}// ✅ Always named imports
import { useState, useEffect, use, useActionState } from 'react';
import { Suspense } from 'react';
// ❌ Never default or namespace imports
import React from 'react';
import * as React from 'react';// ✅ Server Component (default — no directive needed)
async function UserProfile({ userId }: { userId: string }) {
const user = await db.users.findById(userId); // Direct DB access
return <div>{user.name}</div>;
}
// ✅ Client Component — only when you need interactivity
'use client';
function Counter() {
const [count, setCount] = useState(0);
return <button onClick={() => setCount(c => c + 1)}>{count}</button>;
}'use client';
import { use, Suspense } from 'react';
// Read promise in render
function UserData({ promise }: { promise: Promise<User> }) {
const user = use(promise); // Suspends until resolved
return <div>{user.name}</div>;
}
// Usage with Suspense
function App() {
const userPromise = fetchUser(userId);
return (
<Suspense fallback={<Skeleton />}>
<UserData promise={userPromise} />
</Suspense>
);
}
// Conditional context (impossible with useContext)
function ConditionalTheme({ show }: { show: boolean }) {
if (!show) return null;
const theme = use(ThemeContext); // ✅ conditional usage OK
return <div style={{ color: theme.primary }}>themed</div>;
}'use server';
async function createUser(prevState: State, formData: FormData) {
const name = formData.get('name') as string;
if (!name) return { error: 'Name required' };
await db.users.create({ name });
revalidatePath('/users');
return { success: true };
}
// Client Component
'use client';
import { useActionState } from 'react';
function CreateUserForm() {
const [state, action, isPending] = useActionState(createUser, null);
return (
<form action={action}>
<input name="name" />
<button disabled={isPending}>
{isPending ? 'Creating...' : 'Create'}
</button>
{state?.error && <p>{state.error}</p>}
</form>
);
}// ✅ React 19 — ref is a standard prop
function Input({ ref, ...props }: React.InputHTMLAttributes<HTMLInputElement> & {
ref?: React.Ref<HTMLInputElement>
}) {
return <input ref={ref} {...props} />;
}
// ❌ No longer needed
const Input = forwardRef<HTMLInputElement, Props>((props, ref) => (
<input ref={ref} {...props} />
));// ✅ Server Component with parallel fetching
async function Dashboard() {
const [user, posts, stats] = await Promise.all([
fetchUser(),
fetchPosts(),
fetchStats(),
]);
return (
<div>
<UserCard user={user} />
<PostList posts={posts} />
<StatsPanel stats={stats} />
</div>
);
}// ❌ Redundant with React Compiler
const value = useMemo(() => compute(data), [data]);
const handler = useCallback(() => doThing(id), [id]);
// ✅ Simple and direct
const value = compute(data);
const handler = () => doThing(id);// ❌ Makes the entire tree client-side
'use client';
export default function Page() { /* ... */ }
// ✅ Only the interactive component
// page.tsx (Server Component)
export default function Page() {
return (
<div>
<StaticContent />
<InteractiveWidget /> {/* 'use client' only here */}
</div>
);
}| Feature | React 18 | React 19 |
|---|---|---|
| Memoization | Manual useMemo/useCallback | Automatic (Compiler) |
| Promises | useEffect + useState | use() hook |
| Forms | onSubmit handler | Server Actions + useActionState |
| Refs in components | forwardRef | ref as prop |
| Conditional context | ❌ Not possible | ✅ use() |
useMemouseCallback'use client'forwardRefrefforwardRefuse()useContextuseActionStateuseStateuseEffect