react-19 について
React 19 開発者向けのスキルガイドです。コンポーネント、インポート、データ処理の規約を示し、Server Components、Actions、use() フック、ref の props 化を扱います。
主な機能
- 01React Compiler のメモ化指針
- 02名前付きインポートの規約
- 03Server Components を優先
- 04use() による Promise の読み取り
- 05Actions と useActionState
- 06ref を props として渡す方法
活用シーン
- 01クライアントコンポーネントを使う場面の判断
- 02コンポーネント内での Promise の読み取り
- 03Actions を使ったフォーム送信処理
- 04関数コンポーネントへの ref の受け渡し
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ prowler-cloud/ prowler --skill react-19インストール後、Claude Code で/react-19を入力して読み込まれたか確認します。
詳細
手動でのメモ化は不要(必須)
// ✅ React Compiler handles optimization automatically
function Component({ items }) {
const filtered = items.filter(x => x.active);
const sorted = filtered.sort((a, b) => a.name.localeCompare(b.name));
const handleClick = (id) => {
console.log(id);
};
return <List items={sorted} onClick={handleClick} />;
}
// ❌ NEVER: Manual memoization
const filtered = useMemo(() => items.filter(x => x.active), [items]);
const handleClick = useCallback((id) => console.log(id), []);
インポート(必須)
// ✅ ALWAYS: Named imports
import { useState, useEffect, useRef } from "react";
// ❌ NEVER
import React from "react";
import * as React from "react";
まずサーバーコンポーネントを使う
// ✅ Server Component (default) - no directive
export default async function Page() {
const data = await fetchData();
return <ClientComponent data={data} />;
}
// ✅ Client Component - only when needed
"use client";
export function Interactive() {
const [state, setState] = useState(false);
return <button onClick={() => setState(!state)}>Toggle</button>;
}
「use client」を使う場合
- useState、useEffect、useRef、useContext
- イベントハンドラー(onClick、onChange)
- ブラウザー API(window、localStorage)
use() フック
import { use } from "react";
// Read promises (suspends until resolved)
function Comments({ promise }) {
const comments = use(promise);
return comments.map(c => <div key={c.id}>{c.text}</div>);
}
// Conditional context (not possible with useContext!)
function Theme({ showTheme }) {
if (showTheme) {
const theme = use(ThemeContext);
return <div style={{ color: theme.primary }}>Themed</div>;
}
return <div>Plain</div>;
}
アクションと useActionState
"use server";
async function submitForm(formData: FormData) {
await saveToDatabase(formData);
revalidatePath("/");
}
// With pending state
import { useActionState } from "react";
function Form() {
const [state, action, isPending] = useActionState(submitForm, null);
return (
<form action={action}>
<button disabled={isPending}>
{isPending ? "Saving..." : "Save"}
</button>
</form>
);
}
ref を props として渡す(forwardRef は不要)
// ✅ React 19: ref is just a prop
function Input({ ref, ...props }) {
return <input ref={ref} {...props} />;
}
// ❌ Old way (unnecessary now)
const Input = forwardRef((props, ref) => <input ref={ref} {...props} />);
コメント
Markdown 対応