Cursor Rules: React + TypeScript — Component & Hooks Patterns
Cursor rules for React with TypeScript. Enforces functional components, hooks patterns, proper typing, and testing conventions.
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.
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.
How to use
- Create the rules directory in your project:
mkdir -p .cursor/rules
- Create the rule file at
.cursor/rules/react-ts.mdc.
- Add your React + TypeScript conventions. Cursor auto-loads the file on the next session.
Example
A sample .cursor/rules/react-ts.mdc file:
---
description: React + TypeScript component and hooks patterns
globs: ['**/*.tsx', '**/*.ts']
alwaysApply: false
---
# React + TypeScript Rules
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
Hooks
- List all dependencies in useEffect/useMemo/useCallback arrays
- Extract custom hooks for reusable stateful logic
- Prefix custom hooks with 'use'
Typing
- Never use 'any'; prefer 'unknown' and narrow with type guards
- Use discriminated unions for component variants
- Type event handlers explicitly: React.ChangeEvent<HTMLInputElement>
Testing
- Use React Testing Library over Enzyme
- Test behavior, not implementation details
- Use screen.getByRole over getByTestId
Related on TokRepo
- AI Tools for Coding — more AI-assisted coding tools and editor integrations
- Prompt Library — curated prompts and configuration files for AI workflows
Common pitfalls
- Setting
alwaysApply: trueloads the rules for every file type, including non-React files; use glob patterns to restrict activation to.tsxand.tsfiles - 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
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.
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.
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.
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.
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)
- Cursor Docs— Cursor rules documentation and .mdc format
- React TypeScript Cheatsheet— React TypeScript best practices
- Testing Library Docs— React Testing Library guidelines
Related on TokRepo
Source & Thanks
Curated from the cursor.directory community.
Discussion
Related Assets
Conda — Cross-Platform Package and Environment Manager
Install, update, and manage packages and isolated environments for Python, R, C/C++, and hundreds of other languages from a single tool.
Sphinx — Python Documentation Generator
Generate professional documentation from reStructuredText and Markdown with cross-references, API autodoc, and multiple output formats.
Neutralinojs — Lightweight Cross-Platform Desktop Apps
Build desktop applications with HTML, CSS, and JavaScript using a tiny native runtime instead of bundling Chromium.