🐕

React開発環境構築におけるClaudeカスタムインストラクション

に公開

はじめに

AIエージェントによるコーディングで、Claude Codeを利用してアプリ作成をいくつか試すことができたので、次のステップとしてカスタムインストラクションファイル(claude.md)を記載した上で、簡易なアプリケーションを作成して動作確認する。

作成するアプリケーション

作成するアプリケーションはAPIを叩くだけの簡易なものとする。裏側のAPIが比較的重い処理を想定して、レスポンス待ちの状態を確認したい。

  • 利用APIに記載されたJSON placeholderにアクセスしてレスポンス結果を表示するアプリケーション。APIレスポンス待ちに伴うアプリケーションの動作を確認するため、指定された秒数だけAPIレスポンス返却を待機する。指定された秒数はトップ画面の指定秒数項目から、選択できる。選択できる値は、0秒、5秒、10秒、15秒、20秒、25秒、30秒より選択する。トップページのAPIアクセスボタンを押下したら、APIアクセスを開始する。
  • APIアクセスでレスポンス待ちの状態は、Suspenseコンポーネントを利用して、ローディングが完了するまで、fallback propsに指定したJSX要素を表示する。fallbackのページも合わせて作成する。
  • APIはJSON placeholderの以下のAPIを利用する。

カスタムインストラクションファイルの要件

Reactでフロントエンドの実装を行う前提で要件をまとめた。ルールは思いついた順に記載した。

前提要件

  • Reactバージョン: React18以降のバージョンを対象とする。
  • 以下コマンドでビルドした環境で利用する。
    • npm create vite@latest react_application -- --template react-ts

ルール

  • データ取得ライブラリは「@tanstack/react-query」を採用
  • 画面は「pages」フォルダ配下に、ページ遷移関連は「router」フォルダ配下に設置
  • APIのレスポンスの返却値の型を検証する。型定義は、「types」フォルダ配下に設置
  • 外部システムへのAPI発行はカスタムフック経由で実施する。カスタムフックは「hooks」フォルダ配下に設置。
  • カスタムフック経由のAPIアクセスでレスポンス待ちの状態は、Suspenseコンポーネントを利用して、ローディングが完了するまで、fallback propsに指定したJSX要素を表示する。fallbackのページも合わせて作成する。
  • 404ページを作成
  • パフォーマンス測定後、必要に応じてmemo化を適用
  • React DevTools Profilerでレンダリング頻度を確認してから最適化する。
  • 型安全性及びコード可読性のため、受け渡すPropsの型は全て定義する。
  • Atomic Designを採用
  • UIコンポーネントライブラリはMaterial UIを採用
  • useEffect使用は、以下の原則に従うこと。
    • 外部システム、外部APIとの同期のみに使用
    • 可能な限りレンダー中に値を計算
    • ユーザーアクションはイベントハンドラで処理
    • 明確なコメントで同期対象を明記
  • エラーハンドリング
    • APIエラーは統一されたエラーコンポーネントで表示
    • 開発環境では詳細なエラー情報を表示

カスタムインストラクションファイルの作成

上記の要件をもとにClaudeCodeにカスタムインストラクションファイルを作成してもらう。
https://github.com/hiroharu8864/react-api-test/blob/main/claude.md

CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

  • Development Commands
    • Start development server: npm run dev
    • Build for production: npm run build
    • Lint code: npm run lint
    • Preview production build: npm run preview
  • Architecture & Technology Stack
    • Frontend Framework: React 19.1.1 with TypeScript
    • Build Tool: Vite 7 with Hot Module Replacement (HMR)
    • Bundler Mode: ESNext modules with bundler resolution
  • TypeScript Configuration
    • Strict mode enabled with comprehensive linting rules
    • Project references: Split configuration between app (tsconfig.app.json) and build tools (tsconfig.node.json)
    • Target: ES2022 with DOM libraries
    • JSX: react-jsx transform (no React import needed in components)
  • ESLint Configuration
    • Modern setup: Uses new ESLint config format with defineConfig
    • React-specific rules: Includes react-hooks and react-refresh plugins
    • TypeScript integration: typescript-eslint for TypeScript-specific linting
    • Ignores: dist folder is globally ignored
  • Key Development Notes
    • React 19: Uses new createRoot API from react-dom/client
    • Module type: ESM modules (type: "module" in package.json)
    • No test framework: Currently no testing setup configured
    • Standard Vite template: Based on React + TypeScript + Vite template without React Compiler

React Development Guidelines

  • Library Requirements
    • Data fetching: Use @tanstack/react-query for all data fetching operations
    • UI components: Material UI component library
    • Architecture: Atomic Design pattern (atoms, molecules, organisms)
  • Folder Structure
src/
├── components/         # 再利用可能なUIコンポーネント
│   ├── atoms/          # 基本的な構成要素
│   ├── molecules/      # シンプルなコンポーネントの組み合わせ
│   └── organisms/      # 複雑なコンポーネントの組み合わせ
├── pages/              # ページレベルコンポーネント
├── router/             # ルーティング設定
├── hooks/              # カスタムReactフック
├── utils/              # ユーティリティ関数
├── api/                # API呼び出し専用
├── types/              # TypeScript型定義
└── assets/             # 静的アセット
  • Development Rules
    • Type safety: Define types for all props and API responses in types/ folder
    • API access: Use custom hooks in hooks/ folder for external API calls
    • Loading states: Use Suspense component with fallback for async operations
    • Error handling: Create 404 page for route errors
    • Performance: Apply memoization only after React DevTools Profiler analysis
  • useEffect Guidelines
    • Usage scope: Only for external system/API synchronization
    • Computation: Calculate values during render when possible
    • User actions: Handle in event handlers, not useEffect
    • Documentation: Add clear comments explaining synchronization targets

References

https://github.com/hiroharu8864/react-api-test
https://react-api-test-umber.vercel.app/

Discussion