react-19 について
React アプリを開発する人向けの React 19 スキルです。コンパイラー最適化、Server Components、フック、Actions、インポート、ref の扱いを扱います。
主な機能
- 01React Compiler による最適化指針
- 02Server Components 優先のパターン
- 03React 19 の use() と Actions
- 04名前付きインポートの規約
- 05ref をプロパティとして扱う指針
活用シーン
- 01プロジェクトで React 19 の規約を適用
- 02クライアントコンポーネントを使う場面の判断
- 03Promise とフォーム Actions の処理
- 04ref プロパティを使うコンポーネントへの更新
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ gentleman-programming/ gentleman-skills --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(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} />);
キーワード
react、react 19、コンパイラー、useMemo、useCallback、サーバーコンポーネント、use フック
コメント
Markdown 対応