Prompt Engineering for React Component Generation: Templates That Actually Work
Battle-tested prompt templates for generating production-ready React components with AI. Learn the CRAFT framework for writing prompts that produce TypeScript-strict, accessible, and well-structured components on the first try.
Most developers prompt AI tools like they are searching Google: vague, hoping for the best. "Make me a dashboard component" produces generic, often broken code. But a well-structured prompt consistently generates production-ready React components on the first attempt. After generating hundreds of components with Claude and GPT-4, I distilled my approach into repeatable templates.
The CRAFT Framework
Every effective component generation prompt needs five elements: Context, Requirements, API surface, Framework constraints, and Testing notes. Skip any one of these and you will spend more time fixing the output than you saved generating it.
Template 1: Data Display Component
This template works for tables, lists, cards, and any component that renders a collection:
Generate a React Server Component in TypeScript.
Context: E-commerce admin dashboard showing recent orders.
Requirements:
- Display a list of orders with columns: Order ID, Customer, Total, Status, Date
- Status shown as colored badge (pending=yellow, shipped=blue, delivered=green)
- Sorted by date descending, paginated (10 per page)
- Empty state when no orders exist
- Loading skeleton matching the layout
API Surface:
- Props: { orders: Order[]; totalCount: number; currentPage: number }
- Order type: { id: string; customerName: string; total: number; status: "pending" | "shipped" | "delivered"; createdAt: Date }
Framework:
- Next.js 15 App Router, Server Component (no "use client")
- Tailwind CSS + shadcn/ui Table component
- Use cn() from @/lib/utils for conditional classes
- Format currency with Intl.NumberFormat
- Format dates with date-fns formatDistanceToNow
Testing: Include a Storybook story with mock data for each state (loading, empty, populated)
Template 2: Interactive Form Component
Forms are where AI generation fails most often without detailed prompts. This template handles the complexity:
Generate a client component for a multi-step checkout form.
Context: SaaS billing page, step 2 of 3 (payment details).
Requirements:
- Fields: card number, expiry (MM/YY), CVC, billing name
- Real-time validation with error messages below each field
- Card type detection (Visa, Mastercard) with icon
- Submit button disabled until all fields valid
- Loading state during submission with spinner
API Surface:
- Props: { onSubmit: (data: PaymentData) => Promise; onBack: () => void }
- PaymentData: { cardNumber: string; expiry: string; cvc: string; billingName: string }
Framework:
- React Hook Form + Zod validation schema
- shadcn/ui Input, Button, Label components
- Tailwind CSS, mobile-first responsive
- Use "use client" directive
Validation Rules:
- Card number: 16 digits, Luhn algorithm check
- Expiry: MM/YY format, not in the past
- CVC: 3-4 digits
- Name: 2-100 characters, letters and spaces only
Why Specificity Matters
The difference between "make a form" and the template above is the difference between 20 minutes of cleanup and zero minutes of cleanup. Specific prompts produce specific code. Every ambiguity in your prompt is a coin flip the AI might get wrong.
Common Prompt Anti-Patterns
Avoid these mistakes that produce poor component output: asking for too many features in one prompt, not specifying the component type (client vs. server), omitting the props interface, not mentioning your styling framework, and forgetting to specify error and loading states.
Key Takeaways
- Use the CRAFT framework: Context, Requirements, API surface, Framework constraints, Testing
- Always specify the props interface and types — this alone prevents 50% of generation errors
- Include error, loading, and empty states in every component prompt
- Mention your specific tech stack (shadcn/ui, Tailwind, React Hook Form) — generic prompts get generic code
- Keep one prompt per component — multi-component prompts produce tangled code
Admin
Bình luận (0)
Đăng nhập để bình luận
Chưa có bình luận nào. Hãy là người đầu tiên!