作者分享30个在Cursor中使用的battle-tested提示词,覆盖组件构建、状态管理、测试、重构和调试,并解释每个提示词生效的原因。
打开 Cursor,新建一个 React 组件,三十秒后你就会陷入重复输入那些已经写过上百次的样板代码:props 接口、加载状态、错误边界、表单验证。
这是每个 React 开发者都在付出的隐性成本。问题不在于工作本身有多难——而在于它太重复了,重复的工作最容易让时间无声无息地溜走。
Cursor 能改变这一点,但前提是你得知道怎么用它。大多数开发者把 Cursor 当作一个高级自动补全工具。只有一小部分开发者把它当作身边坐着一位资深工程师——而那一小部分人用几分之一的时间就能交付功能。
本文包含 30 个经过实战检验的 Cursor 提示词,我每周都在用它们来加速组件构建、状态管理、测试、重构和调试。每一个提示词都可以直接复制使用,同时我会解释每个提示词为什么有效,这样你就能把它们适配到你自己的代码库中。
读完本文,你会拥有一套可复用的提示词库——而不是一堆零散的技巧。
大多数开发者从 Cursor 获得糟糕的结果,不是因为工具本身弱,而是因为提示词太模糊。
以下是通常会出的问题:
提示词太过笼统——"让这个组件更好"没有给 Cursor 任何可以优化的方向。
没有提供上下文——除非你明确告诉 Cursor,否则它不会知道你的文件夹结构、状态管理库或样式方案。
一次只用一个巨大的提示词而不是分步骤聚焦——一次性请求整个功能会产生混乱且难以审查的代码。
没有约束条件——没有规则(TypeScript、可访问性、性能),Cursor 就会采用最简单可能的实现。
解决方案不是更智能的 AI,而是结构化的提示——把 Cursor 当作需要明确指示的队友,而不是会读心术的人。
与临时请求不同,下面的提示词遵循可重复的模式:
一旦你内化了这个模式,你就能随时编写自己的提示词。但为了节省你的时间,这里有 30 个现在就能直接使用的提示词。
1. 生成一个带类型的函数式组件
Create a React functional component named `UserCard` using TypeScript.
Props: name (string), avatarUrl (string), isOnline (boolean).
Use Tailwind CSS for styling. Include proper prop types and default export.
2. 将类组件转换为 Hooks
Refactor this class component into a functional component using React hooks.
Preserve all existing behavior and lifecycle logic (componentDidMount, componentWillUnmount).
3. 构建一个可复用的按钮组件
Create a reusable `Button` component supporting variants: primary, secondary, danger.
Support a `loading` state that disables the button and shows a spinner.
Use TypeScript and forwardRef.
4. 生成骨架屏加载状态
Create a skeleton loading component that matches the layout of this component: [paste component].
Use Tailwind's animate-pulse utility.
5. 创建复合组件模式
Refactor this Tabs component into a compound component pattern
(Tabs.List, Tabs.Panel, Tabs.Trigger) using React Context internally.
6. 干净地添加本地状态
Add local state to manage a multi-step form (step index, form data, validation errors).
Use useReducer instead of multiple useState calls if there are more than 3 related values.
7. 设置 Zustand store
Create a Zustand store for managing authenticated user state.
Include: user, isAuthenticated, login(), logout(), and persist to localStorage.
8. 调试不必要的重新渲染
Analyze this component and identify why it re-renders too often.
Suggest fixes using React.memo, useCallback, or useMemo where appropriate.
Explain each fix in one sentence.
9. 将 prop drilling 转换为 Context
Refactor this prop drilling pattern into a React Context provider.
Keep the API surface backward-compatible for existing consumers.
10. 添加乐观 UI 更新
Add optimistic update logic to this mutation using React Query.
Roll back on error and show a toast notification.
11. 为端点设置 React Query
Create a custom hook `useUsers` using React Query (TanStack Query) that fetches
from GET /api/users. Include loading, error, and typed response handling.
12. 给数据获取添加分页
Add cursor-based pagination to this React Query hook.
Support "load more" and infinite scroll using useInfiniteQuery.
13. 优雅地处理 API 错误
Add error handling to this fetch call. Show a user-friendly error message,
log the raw error to the console, and add a retry button.
14. 从 API 响应生成 TypeScript 类型
Here is a sample JSON API response: [paste JSON].
Generate matching TypeScript interfaces with proper naming conventions.
15. 添加请求取消功能
Add AbortController support to this fetch function so requests are
cancelled when the component unmounts.
16. 构建带验证的表单
Create a login form using react-hook-form and Zod for validation.
Fields: email (required, valid email), password (min 8 characters).
Show inline error messages.
17. 添加异步验证
Add async validation to this signup form to check if the email
already exists via an API call, with debounce.
18. 创建可复用的表单字段组件
Create a reusable `FormField` component that wraps a label, input, and
error message, compatible with react-hook-form's register function.
19. 生成单元测试
Write unit tests for this component using React Testing Library and Vitest.
Cover: default render, loading state, error state, and click interaction.
20. 为自定义 hook 生成测试
Write tests for this custom hook using @testing-library/react-hooks.
Include cases for success, error, and loading state transitions.
21. 添加可访问性测试
Add accessibility tests using jest-axe to check this component
for WCAG violations.
22. 审查组件的性能问题
Review this component for performance problems: unnecessary re-renders,
missing memoization, large bundle imports. List issues by priority.
23. 懒加载重型组件
Convert this component to be lazy-loaded using React.lazy and Suspense.
Add a fallback loading skeleton.
24. 优化大型列表渲染
This list renders 1000+ items and is slow. Refactor it to use
virtualization with react-window or @tanstack/react-virtual.
25. 解释堆栈跟踪
Here is an error stack trace: [paste error].
Explain what's causing it in plain English and suggest the exact fix.
26. 找出内存泄漏的源头
Review this component for memory leaks (uncancelled subscriptions,
timers, or event listeners left after unmount).
27. 调试 hydration 不匹配
This Next.js component throws a hydration mismatch error.
Identify the cause and suggest a fix that keeps server and client output identical.
28. 简化凌乱的组件
Refactor this component to improve readability without changing behavior.
Extract logic into custom hooks where appropriate. Keep functions under 20 lines.
29. 添加正确的 TypeScript 类型
This file uses `any` in several places. Replace all `any` types with
proper, specific TypeScript types based on how the values are used.
30. 生成 JSDoc 文档
Add JSDoc comments to this file's exported functions and components,
including parameter and return type descriptions.
1. 粘贴整个文件却不说目标
Cursor 会"改进"你没有要求的部分,产生不必要的差异。
2. 不指定技术栈
如果不提 TypeScript、Tailwind 或你的状态库,Cursor 就会猜测——而且经常猜错。
3. 不阅读就接受代码
AI 生成的代码可能看起来正确,但仍然包含细微的 bug,尤其是在异步状态和 effect 依赖方面。
4. 用一个巨大的提示词请求整个功能
大而模糊的提示词会产生大而难以审查的 PR。把工作分成步骤来做。
5. 忽略现有规范
如果不告诉 Cursor 你的命名或文件夹规范,它就不会遵循——导致代码库不一致。
始终说明技术栈:React 版本、TypeScript 还是 JS、样式方案(Tailwind、CSS Modules、styled-components)。
一次只问一件事:组件、然后测试、然后重构——不要三件事一起问。
提前给出约束:"无外部库"、"必须支持 SSR"、"保持 bundle 体积小"。
让 Cursor 解释它的修改:加上"解释你改了什么以及为什么",以发现静默的副作用。
使用 .cursorrules:一次性定义项目规范,这样每个提示词都会自动继承它们。
像审查 PR 一样审查差异:把 Cursor 的输出当作初级开发者的 PR,而不是成品。
(用于 Medium 发布,在此插入截图)
截图 1:Cursor 的聊天面板,显示正在输入上面的一个提示词,AI 响应在代码编辑器旁边流式输出。
截图 2:前后对比的 diff 视图,显示一个类组件被重构为基于函数式 hook 的组件。
截图 3:编辑器中打开的 .cursorrules 文件,显示示例项目规范(命名、文件夹结构、测试规则)。
截图 4:文件夹结构图,显示 components/、hooks/、lib/ 和 tests/ 在 Cursor 辅助重构后组织清晰。
这些提示词不是理论性的——它们直接对应你在生产代码库中会遇到的情况:
SaaS 仪表盘:生成带类型的数据表格、处理分页和为设置页面构建表单验证。
管理后台:用一致的样式和错误处理快速脚手架化 CRUD 组件。
移动优先的 Web 应用:优化列表渲染和懒加载重型组件,以在较慢的设备上提升性能。
电商平台:构建带异步验证和乐观 UI 更新的健壮结账表单。
内部工具:为快速交付但仍需可靠性的组件快速编写测试。
Cursor 不是通过替你写代码来节省时间——而是当你确切知道如何要求你所需的东西时,它才能节省你的时间。
本文中的 30 个提示词覆盖了真实 React 开发中消耗最多时间的情况:组件脚手架、状态管理、数据获取、表单、测试、性能和调试。
从这个列表开始,把它保存在一个标签页中。每当你遇到重复性任务时,借用这个模式而不是从零开始输入。几周后,你会注意到你的提示风格从模糊的请求转变为精确的、资深工程师级别的指令——而你的产出也会随之提升。
学习 React 是一回事,高效地构建现实世界的应用是另一回事。
你可以用 ChatGPT、Claude 和 Cursor 写代码、调试问题、重构组件、生成功能并加速开发,但获得有用的结果严重依赖于你如何提示 AI。
这就是为什么我创建了 The Ultimate React + Cursor Prompt Library(1000+ AI 提示词),这是一套专为 React 开发者设计的实用 AI 提示词集合。
在这个库中,你会找到 1000+ 个实用提示词,涵盖 React 开发、UI 组件、调试、重构、性能优化、API 集成、状态管理、测试、架构等。
每个提示词都旨在帮助你从 Cursor、ChatGPT 和 Claude 等 AI 编程助手那里获得更好的结果,这样你就能少花时间思考该问什么,多花时间构建。
与其盯着空白的 Cursor 聊天窗口想着该写什么提示词,不如从经过验证的提示词开始,并把它们适配到你自己的项目中。
无论你在构建 SaaS、自由职业项目、创业公司项目、仪表盘还是个人应用,这个库都能帮助你更快地编写代码、更快地解决问题,并从 AI 辅助开发中获得更多收益。
👉 The Ultimate React + Cursor Prompt Library:1000+ AI 提示词 →