ConfigsMar 29, 2026·1 min read

Cursor Rules: React + TypeScript — Component & Hooks Patterns

Cursor rules for React with TypeScript. Enforces functional components, hooks patterns, proper typing, and testing conventions.

TL;DR
Cursor rule file that teaches AI to write production-quality React + TypeScript code.
§01

What it is

This is a Cursor rule configuration file (.mdc) that teaches Cursor AI to write production-quality React code with TypeScript. It enforces functional component patterns, hooks best practices, proper typing for props and state, and testing conventions. When placed in your project's .cursor/rules/ directory, Cursor applies these patterns to every suggestion.

The target audience is React developers who use Cursor as their editor and want consistent, well-typed code suggestions without repeatedly correcting the AI's output.

§02

How it saves time or tokens

Without framework-specific rules, Cursor's AI may generate class components, use any types, or ignore hooks rules like the dependency array. Correcting these mistakes burns tokens and interrupts flow. This rule file front-loads the conventions so the first suggestion is more likely to be correct. You spend fewer turns on stylistic corrections and more on actual logic.

§03

How to use

  1. Create the rules directory in your project:
mkdir -p .cursor/rules
  1. Create the rule file at .cursor/rules/react-ts.mdc.
  1. Add your React + TypeScript conventions. Cursor auto-loads the file on the next session.
§04

Example

A sample .cursor/rules/react-ts.mdc file:

---
description: React + TypeScript component and hooks patterns
globs: ['**/*.tsx', '**/*.ts']
alwaysApply: false
---

# React + TypeScript Rules
§05

Components

  • Always use functional components with arrow syntax
  • Define props as a named interface, not inline type
  • Use React.FC only when children are expected
  • Destructure props in the function signature
§06

Hooks

  • List all dependencies in useEffect/useMemo/useCallback arrays
  • Extract custom hooks for reusable stateful logic
  • Prefix custom hooks with 'use'
§07

Typing

  • Never use 'any'; prefer 'unknown' and narrow with type guards
  • Use discriminated unions for component variants
  • Type event handlers explicitly: React.ChangeEvent<HTMLInputElement>
§08

Testing

  • Use React Testing Library over Enzyme
  • Test behavior, not implementation details
  • Use screen.getByRole over getByTestId
§09

Related on TokRepo

§10

Common pitfalls

  • Setting alwaysApply: true loads the rules for every file type, including non-React files; use glob patterns to restrict activation to .tsx and .ts files
  • Overly prescriptive rules may conflict with AI suggestions, causing garbled output; keep rules at the guideline level, not line-by-line templates
  • Rules do not override Cursor's model knowledge entirely; if the AI suggests something contradicting your rules, it may be responding to context in your codebase that conflicts with the rule

Frequently Asked Questions

Where do I place this file in my project?+

Place the .mdc file in the .cursor/rules/ directory at your project root. Cursor automatically discovers and loads all .mdc files from this directory. The filename can be anything descriptive, like react-ts.mdc.

Can I have multiple rule files active at once?+

Yes. Cursor loads all .mdc files from the rules directory. You can have separate files for React, testing, API patterns, and styling conventions. Use glob patterns to control which file types each rule applies to.

Do these rules work with JavaScript (non-TypeScript) React?+

The TypeScript-specific rules (typing, interfaces) would not apply to .jsx files. You can create a separate rule file for JavaScript React projects or adjust the glob pattern to include .jsx and remove the TypeScript typing rules.

How do I update rules when my team's conventions change?+

Edit the .mdc file directly. Cursor reloads rules on session start, so changes take effect the next time you open the project. Version-control the .cursor/rules/ directory to share conventions across the team.

Will these rules work in VS Code with Copilot?+

No. The .mdc format is specific to Cursor. VS Code with GitHub Copilot uses a different configuration mechanism. However, the conventions documented in the .mdc file can inform a .github/copilot-instructions.md file for Copilot.

Citations (3)
🙏

Source & Thanks

Curated from the cursor.directory community.

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets