debugging
Debug a React state bug with a systematic root-cause prompt
Forces a systematic root-cause investigation of a React state or rendering bug instead of a surface-level patch that might mask the real issue.
Who should use this
Is this prompt for you?
- Developers stuck on a React bug that only reproduces sometimes
- Anyone getting stale-state, infinite-loop, or unexpected re-render bugs
- Teams that want the AI to explain the bug, not just silently patch it
The prompt
Copy this prompt
Prompt
I have a bug in a React component. Help me find the root cause — do not just patch the symptom. Component code: [paste the relevant component(s), including any custom hooks and context providers involved] Bug description: - What I expected to happen: [describe] - What actually happens: [describe] - Steps to reproduce: [describe, including how often it reproduces — always / intermittently / only on specific actions] - Relevant console errors or warnings: [paste exactly, or say "none"] Tech context: React [version], [state management approach], [relevant libraries like React Query, Redux, Zustand]. Please work through this systematically: 1. Trace the component's render and state-update flow step by step, based only on the code shown — don't assume behavior you can't see 2. Identify every place state could become stale, be set based on stale closures, or update in an order that causes the described symptom 3. Check for classic causes: missing/incorrect dependency arrays, state updates in render, derived state that should be computed instead of stored, mutating state directly, race conditions between async updates 4. State your most likely root cause and your confidence level, with your reasoning 5. If there are multiple plausible causes, list them ranked by likelihood instead of guessing one and moving on 6. Propose the minimal fix for the actual root cause — not a broader rewrite 7. Suggest a test that would have caught this bug, and that will catch a regression If the code I've shown isn't enough to be sure, tell me exactly what additional code or logs you need instead of guessing.
Works well with Claude Code, Cursor and GitHub Copilot.
How to use it
Getting the best result
- 1Paste the actual component code, not a paraphrase — exact code matters for finding closures and dependency issues
- 2Be precise about reproduction steps and frequency (always vs. intermittent points to very different bug classes)
- 3If the AI asks for more code or logs, provide them rather than pushing it to guess
- 4Apply only the minimal fix first, then verify the bug is actually gone before accepting any broader refactor
Expected result
What you should get back
- A step-by-step trace of the render/state flow based on your actual code
- A ranked list of likely root causes with reasoning, not a single guess
- A minimal, targeted fix rather than an unrelated rewrite
- A suggested regression test for the specific bug
Tips
Get more out of this prompt
- Intermittent bugs are almost always race conditions or stale closures — say explicitly if the bug is intermittent
- Include any relevant custom hooks and context providers, not just the component where the symptom shows up
- If the first fix doesn't work, report back exactly what you observed rather than starting a new unrelated prompt
Common mistakes
What to watch out for
- Only pasting the component that shows the symptom, omitting a parent or hook that's the actual cause
- Accepting the first proposed fix without asking why the bug happened in the first place
- Describing the bug vaguely ("it doesn't work") instead of exact expected vs. actual behavior
Related prompts
Keep going
react
Refactor a messy React component into clean, reusable pieces
Guides an AI to safely refactor a large, tangled React component into smaller composable pieces while preserving exact behavior.
Copy promptdebuggingDiagnose and fix a Next.js hydration mismatch error
Walks an AI through correctly diagnosing a Next.js hydration mismatch and fixing the root cause, instead of silencing it with suppressHydrationWarning.
Copy prompttestingGenerate a thorough unit test suite for existing code
Generates a unit test suite that actually covers edge cases and failure paths for existing code, instead of only testing the happy path.
Copy promptReady to put this prompt to work?
Pair it with an AI coding tool and a real project, then ship something this week.