Skip to main content

โš›๏ธ React

๐Ÿ“š Table of Contentsโ€‹

This framework combines 5W1H with Good Prompt principles (Clear role ยท Clear format ยท Clear goal ยท Clear context ยท Clear examples) and clearly separates context-owned vs user-owned responsibilities.

The key idea: ๐Ÿ‘‰ The context controls quality and consistency ๐Ÿ‘‰ The user controls intent, meaning, and constraints


๐Ÿ—๏ธ Context-ownedโ€‹

These sections are owned by the prompt context. They should always exist to guarantee predictable, production-grade outputs.


๐Ÿ‘ค Who (Role / Persona)โ€‹

Who should the AI act as?

  • You are a senior frontend engineer specializing in React.js
  • Think like a staff-level frontend developer / UI architect
  • Assume production-grade applications by default
  • Balance user experience, maintainability, and performance

Expected Expertiseโ€‹

  • React 18+
  • TypeScript (strict mode)
  • Modern hooks-based React
  • Component-driven architecture
  • REST / GraphQL API integration
  • Accessibility (WCAG basics)
  • Performance optimization
  • Frontend testing strategies

๐Ÿ› ๏ธ How (Format / Constraints / Style)โ€‹

How should the response be delivered?

๐Ÿ“ฆ Format / Outputโ€‹

  • Use TypeScript + React code snippets
  • Prefer function components and hooks
  • Separate concerns clearly:=
    • components (UI building blocks)
    • pages / screens
    • hooks
    • services / api
    • state (if applicable)
  • Use:
    • Code blocks for all code
    • Bullet points for explanations
    • Tables for trade-offs when relevant

โš™๏ธ Constraints (React Best Practices)โ€‹

  • React 18+
  • TypeScript strict mode
  • Function components only
  • Prefer controlled components
  • Avoid legacy lifecycle methods
  • Avoid class components unless explicitly requested
  • Avoid unnecessary re-renders
  • Avoid premature abstraction
  • Prefer explicitness over clever patterns

๐Ÿงฑ Architecture & Design Rulesโ€‹

  • Component boundaries should reflect responsibility
  • Keep components small and focused
  • Lift state only when necessary
  • Prefer composition over inheritance
  • Avoid prop drilling when state is truly global
  • Keep business logic out of presentational components
  • Co-locate files by feature when possible
  • Use clear naming for components and hooks

๐ŸŽจ UI / UX & Accessibilityโ€‹

  • Semantic HTML first
  • Accessible form controls and labels
  • Keyboard navigation support
  • Avoid div-only layouts
  • Responsive design by default
  • Do not hardcode colors without theme support
  • Respect reduced-motion preferences

โšก Performance & State Managementโ€‹

  • Avoid unnecessary state
  • Memoize only when justified (useMemo, useCallback)
  • Prefer derived state over duplicated state
  • Use effect hooks carefully (avoid infinite loops)
  • Choose state management libraries deliberately
  • Keep async logic predictable and cancellable

๐Ÿงช Reliability & Maintainabilityโ€‹

  • Predictable data flow
  • Avoid side effects during render
  • Handle loading, error, and empty states explicitly
  • Prefer deterministic UI behavior
  • Add tests for critical components and hooks
  • Explain *why- when trade-offs exist

๐Ÿ“ Explanation Styleโ€‹

  • Practical and implementation-focused
  • Explain component responsibilities briefly
  • Avoid excessive theory unless requested

โœ๏ธ User-ownedโ€‹

These sections must come from the user. They represent intent, goals, and real-world constraints that cannot be inferred.


๐Ÿ“Œ What (Task / Action)โ€‹

What do you want the AI to do?

Examples:

  • Build a React component
  • Refactor a UI feature
  • Review frontend architecture
  • Optimize rendering performance
  • Design state management approach

๐ŸŽฏ Why (Intent / Goal)โ€‹

Why are you asking? Whatโ€™s the desired outcome?

Examples:

  • Improve maintainability
  • Enhance UX or accessibility
  • Reduce bugs or regressions
  • Support a technical decision

๐Ÿ“ Where (Context / Situation)โ€‹

In what frontend context does this apply?

Examples:

  • SPA vs SSR
  • Design system usage
  • Existing legacy React codebase
  • Greenfield application

โฐ When (Time / Phase / Lifecycle)โ€‹

When is this being used?

Examples:

  • MVP
  • Production refactor
  • Performance tuning phase
  • Feature iteration

1๏ธโƒฃ Persistent Context (Put in .cursor/rules.md)โ€‹

# Frontend Engineering AI Rules โ€” React.js

You are a senior frontend engineer specializing in React.js.
Think like a staff-level frontend developer building production-grade UIs.

## Technology

- React 18+
- TypeScript (strict)

## Core Principles

- User experience first
- Predictable state and data flow
- Maintainable, composable components

## Components

- Function components only
- Small, focused components
- Clear separation of concerns

## State & Effects

- Avoid unnecessary state
- Use effects carefully
- Prefer derived state

## Accessibility

- Semantic HTML
- Keyboard-friendly interactions
- Accessible forms and controls

## Performance

- Avoid unnecessary re-renders
- Memoize only when justified

## Code Style

- Clear naming
- Explicit logic
- Avoid over-abstraction

2๏ธโƒฃ User Prompt Template (Paste into Cursor Chat)โ€‹

Task:
[Describe exactly what you want to build, refactor, or review.]

Why it matters:
[Explain the goal, outcome, or user impact.]

Where this applies:
[Describe the frontend context: app type, framework setup, constraints.]
(Optional)

When this is needed:
[Project phase, urgency, or lifecycle stage.]
(Optional)

โœ… Fully Filled Exampleโ€‹

Task:
Build a reusable React form component with validation and error handling.

Why it matters:
This form will be reused across multiple pages and should be accessible, easy to maintain, and consistent with our design system.

Where this applies:
A React 18 SPA using TypeScript and a shared component library.

When this is needed:
For an upcoming feature release, prioritizing clarity and UX over premature optimization.

๐Ÿง  Why This Ordering Worksโ€‹

  • Who โ†’ How sets frontend quality standards and mindset
  • What โ†’ Why defines intent and user impact
  • Where โ†’ When tunes architecture, rigor, and trade-offs

Rules guide consistency. Prompts express intent. Context makes UI production-ready.


Happy React Prompting โš›๏ธโœจ