Web Development · Front-end
The Best Prompt for Building a React Component
The best prompt for React asks for a design note before any code: which components exist, where each piece of state lives, and which effects are truly needed. This prompt then requires complete files, keyboard-accessible markup and tests for the most important behaviors, and it forbids invented endpoints and unrequested dependencies. You get a component you can read, run and review rather than a snippet with gaps.
React Component Prompt
Makes the model plan components, state and effects before coding, then deliver complete files, tests and a list of what it assumed.
- Works with
- ChatGPT, Claude, Gemini, Cursor
- Difficulty
- Intermediate
- Complexity
- Multi-part reply
- Variables
- 6
- Version
- 1.0
Customize this prompt
Fill in what you know. Anything left blank stays as a [bracketed placeholder]. Nothing you type leaves your browser.
6 variables · fields marked * matter most
# Role You are a senior React engineer. You write components that are easy to test, accessible by default and no more abstract than the job requires. # What I'm building - Component or feature: [Component or feature] - Behavior and requirements: [Behavior and requirements] - Stack: [Stack] - Where the data comes from: [Data source] - Existing code or conventions to follow: [Existing code or conventions] # Part 1: design note (before any code) Keep this short. Cover: 1. Component breakdown: each component, its single responsibility and its props as a type. 2. State: every piece of state, where it lives and why. Name the values that are derived and therefore not stored. 3. Effects: each effect and the external system it synchronizes with. If something can be computed during render or handled in an event handler, do that instead and say so. 4. UI states: loading, empty, error, partial data, plus anything these requirements imply. # Part 2: implementation - Function components and hooks only. - Complete files, each under a heading with its file path. No elided sections and no "rest of the code here". - Semantic HTML first. Add ARIA only where no native element does the job. Every interactive element must be operable by keyboard and show visible focus. - Keys come from stable data ids, never array indexes for lists that can reorder. - Do not add memo, useMemo or useCallback unless you name the specific re-render or cost it prevents. - Use only the libraries in the stack. If another dependency seems warranted, propose it in one line instead of importing it. - Do not invent endpoints, response fields or props of existing components. Where a shape is unknown, define a clearly named placeholder type and mark it with a TODO comment. # Part 3: after the code - Tests ([Testing approach]): list the behaviors worth testing as user-visible scenarios. Unless the approach says scenarios only, write the tests for the three most important. - Assumptions: what you guessed about requirements or data. - Not handled: edge cases you deliberately left out. # Output format Design note, then one code block per file, then Tests, Assumptions and Not handled. Outside code blocks, use short bullets only.
Use it when you are
- Building a new interactive component such as a data table, combobox or multi-step form
- Adding a feature to an existing React app while matching its conventions
- Getting a reviewable first implementation with tests before a pull request
- Checking your own plan for state and effects against a second opinion
Why this prompt works
State and effects are decided on paper first
Most problems in generated React code are structural: duplicated state, values stored that should be derived, effects doing work that belongs in an event handler. The design note makes the model commit to those decisions in a form you can scan in a minute, so you can reject a bad structure before reading two hundred lines built on it.
Effects have to name what they synchronize with
The React documentation describes effects as a way to synchronize with external systems. Asking the model to state the external system for each one gives it a concrete test to apply. An effect with no answer to that question is usually a calculation or an event response, and the prompt tells the model to move it.
Memoization needs a reason
Left alone, models tend to wrap handlers and values in useCallback and useMemo by habit, which adds noise and dependency arrays to maintain. Requiring a named cost for each use keeps the code plain where plain is enough, and leaves a written justification where optimization was actually intended.
Unknowns become TODOs instead of fiction
A component that calls an endpoint the model made up looks finished and fails at runtime. Placeholder types with TODO comments, plus the closing Assumptions and Not handled lists, show you exactly where the generated code meets parts of your system the model never saw.
How to use it
- Describe the feature as user behavior: what someone can click, type and see. Leave implementation choices to the design note.
- List the exact stack with versions and every library that is allowed. Anything not listed will be proposed, not imported.
- Paste the real data types or a sample API response. This is the single input that most reduces placeholder code.
- Read the design note before the code. If the state placement looks wrong, say so and ask for a revised note and implementation.
- Run the code and the tests locally, then work through the Assumptions list and correct anything that does not match your app.
- Test the result with a keyboard and a screen reader yourself; the prompt asks for accessible markup but cannot verify it.
AI model compatibility
| Model | Status | Notes |
|---|---|---|
| ChatGPT | Expected to work | If the reply is long and stops mid-file, ask it to continue from the start of the interrupted file so you do not end up with a half-written component. |
| Claude | Expected to work | Anthropic's guidance recommends separating instructions from supplied material with XML-style tags. Wrap pasted components in <existing_code> tags so they are not read as part of the instructions. |
| Gemini | Expected to work | No model-specific adjustment needed. Paste the prompt as written. |
| Cursor | Expected to work | Inside a repo, replace the pasted code in the existing-code field with the paths of two or three files that show your conventions, and ask for the design note before any edits are applied. |
“Expected to work” means the prompt uses plain, model-independent structure and has been reviewed for that — not that it has been run on that model. A model is marked Tested only when a test record is published below.
Worked example
Scenario: An illustrative case: an internal admin app needs an orders table with sorting, filtering and bulk export.
# Role You are a senior React engineer. You write components that are easy to test, accessible by default and no more abstract than the job requires. # What I'm building - Component or feature: A sortable, filterable orders table with row selection - Behavior and requirements: Sort by date, customer or total. Filter by status (pending, shipped, refunded). Select rows with checkboxes, including select-all for the current page, and export selected rows as CSV. Sort and filter persist in the URL query string. - Stack: React 19, TypeScript, Vite, TanStack Query v5, React Router v7, CSS Modules - Where the data comes from: GET /api/orders?status=&sort=&page= returns { items: Order[], total: number }. Order = { id: string; customer: string; total: number; status: 'pending' | 'shipped' | 'refunded'; createdAt: string } - Existing code or conventions to follow: We have Button, Checkbox and Select components in src/ui. Files are named PascalCase.tsx with a matching .module.css. # Part 1: design note (before any code) Keep this short. Cover: 1. Component breakdown: each component, its single responsibility and its props as a type. 2. State: every piece of state, where it lives and why. Name the values that are derived and therefore not stored. 3. Effects: each effect and the external system it synchronizes with. If something can be computed during render or handled in an event handler, do that instead and say so. 4. UI states: loading, empty, error, partial data, plus anything these requirements imply. # Part 2: implementation - Function components and hooks only. - Complete files, each under a heading with its file path. No elided sections and no "rest of the code here". - Semantic HTML first. Add ARIA only where no native element does the job. Every interactive element must be operable by keyboard and show visible focus. - Keys come from stable data ids, never array indexes for lists that can reorder. - Do not add memo, useMemo or useCallback unless you name the specific re-render or cost it prevents. - Use only the libraries in the stack. If another dependency seems warranted, propose it in one line instead of importing it. - Do not invent endpoints, response fields or props of existing components. Where a shape is unknown, define a clearly named placeholder type and mark it with a TODO comment. # Part 3: after the code - Tests (React Testing Library with Vitest): list the behaviors worth testing as user-visible scenarios. Unless the approach says scenarios only, write the tests for the three most important. - Assumptions: what you guessed about requirements or data. - Not handled: edge cases you deliberately left out. # Output format Design note, then one code block per file, then Tests, Assumptions and Not handled. Outside code blocks, use short bullets only.
What a good response should contain
- A design note that keeps sort and filter in the URL rather than in component state, and treats the selected-row count as derived
- No effect used to copy query data into local state
- A real table element with column headers that expose sort direction and are operable by keyboard
- Loading, empty and error states for the query, each rendered explicitly
- An assumptions list that flags the unknown props of the existing Button, Checkbox and Select components
This is an illustrative scenario showing how to fill in the prompt and what to look for. It is not a recorded model output. Recorded outputs will be published here once this prompt has a test record.
Testing and editorial review
Status: Untested. Reviewed for structure and clarity by the editorial team. No recorded model test yet.
No test record has been published for this prompt yet. When it is tested, this section will show the model and version, the date, the inputs, the actual output and notes against our seven-point rubric. We do not publish numerical scores.
| Author | TheBestPromptFor Editorial Team — The editorial team writes, reviews and maintains the prompts and guides on TheBestPromptFor. Prompts are drafted with AI assistance and reviewed against published editorial standards. |
|---|---|
| Published | November 15, 2026 |
| Last reviewed | November 15, 2026 |
| Last tested | Not yet tested |
| Version history |
|
Limitations and things to check
- The model cannot run the code. Type errors, failing tests and runtime bugs are possible until you execute it.
- Library APIs change between major versions. Check generated calls against the documentation for the versions you use.
- Accessible-looking markup is not the same as an accessible component. Verify with keyboard and assistive technology.
- Without your real types and existing components, large parts of the output will be placeholders.
Alternative prompts
Web Development
Next.js App Router Feature Prompt
A plan-then-build prompt for App Router work: routes, server and client boundaries, caching, mutations and authorization, approved before any code.
- ChatGPT
- Claude
- Gemini
- Cursor
Web Development
Responsive CSS Layout Prompt
Builds a layout with Grid, Flexbox and container queries that adapts to its content, with the reasoning and the edge cases to test.
- ChatGPT
- Claude
- Gemini
- Cursor
Coding
JavaScript and TypeScript Function Prompt
A contract-first prompt: the model fixes the signature, types and error behavior of a function or module before it writes the implementation.
- ChatGPT
- Claude
- Gemini
- Cursor
Coding
Unit Test Writing Prompt
Produces a case inventory first, then readable tests that check behavior at the boundaries, with suspected bugs flagged instead of encoded as passing.
- ChatGPT
- Claude
- Gemini
- Cursor
Related resources
Guide
How to Write a Good AI Prompt
The five parts of a complete prompt (goal, context, task, constraints, output format), with a worked rewrite and a pre-send checklist.
Technical guide
Prompt Chaining: Breaking a Task Into Linked Prompts
How to split a large task into a sequence of focused prompts, design the handoffs between them, and debug the chain when a step goes wrong.
Frequently asked questions
Why ask for a design note instead of just the code?
Because the expensive mistakes are structural. If state is in the wrong component or an effect is doing a handler's job, every line below it inherits the problem. A short note lets you catch that in a minute and redirect the model before it writes the implementation.
Does this prompt work for JavaScript projects without TypeScript?
Yes. State the stack as JavaScript and the model should describe props as a plain list and omit type annotations. You lose the placeholder-type safety net, so read the Assumptions section more carefully for guessed data shapes.
Can I use it for a whole page or an entire app?
It is scoped to a component or a single feature. For a page that involves routing, server rendering and data caching decisions, start with the Next.js feature prompt or a technical spec, then use this prompt for the individual interactive parts.
Sources and references
- React documentation: Learn — React
- Prompt engineering overview — Anthropic
- Web Content Accessibility Guidelines (WCAG) 2.2 — W3C
Was this prompt useful?
No account needed. Do not include personal information.