The Ultimate Cursor Rule for Clean React.js Components
The Ultimate Cursor Rule for Clean React.js Components
Many developers are pivoting from standard editors to Cursor AI, a powerful fork of VS Code. However, left to its own devices, AI code completion often mixes modern React with outdated 2019 patterns, uses inefficient inline styling, and defaults to bad habits like using array indices as unique keys.
You do not need to clean up after the AI manually. By using Cursor's native rule configuration files (.mdc), you can establish architectural guardrails that force the AI to write pristine code on its first try. No extra npm packages are required.
The Recipe Configuration
To get started, create a new folder path in your project root at .cursor/rules/ and name the file react-modern.mdc. Copy and paste the configuration code block below exactly as written:
---
description: Rules for generating modern and clean React.js components
globs: src/**/*.js, src/**/*.jsx, components/**/*.jsx, app/**/*.jsx
---
# Modern React.js Rules
- **Functional Only:** Never generate class components. Use clean arrow functions (`const Component = () => {}`).
- **Export Pattern:** Use explicit named exports instead of default exports to ensure seamless IDE auto-imports.
- **Props Destructuring:** Always destructure props directly inside the component function signature.
- **Keys in Arrays:** Always provide a stable, unique item identifier (`item.id`) as the key prop when mapping arrays. Never default to the array index.
- **Styling:** Avoid inline `style={{}}` attributes. Use utility-first classes (like Tailwind CSS) or semantic layout patterns.
Why it Matters: Before vs After
When you save this .mdc file, Cursor automatically scans your workspace workspace and updates its system prompts for your target files. Here is a direct look at how this changes the output quality:
Before Applying the Rule (Standard AI Output)
Without instructions, LLMs often default to generic, unoptimized code styles:
import React from 'react';
export default function Dashboard(props) {
const user = props.user;
return (
<div style={{ padding: '20px', border: '1px solid #ccc' }}>
<h1>Welcome, {user.name}</h1>
<ul>
{user.items.map((item, index) => (
<li key={index}>{item.name}</li>
))}
</ul>
</div>
);
}
After Applying the Rule (Cursor AI Output)
With your new recipe rule file active in the folder background, the AI naturally adheres to clean architectural principles:
import React from 'react';
export const Dashboard = ({ user }) => {
const { name, items } = user;
return (
<div className="p-5 border border-gray-200 rounded-lg">
<h1 className="text-xl font-bold">Welcome, {name}</h1>
<ul className="mt-4 space-y-2">
{items.map((item) => (
<li key={item.id} className="text-gray-700">
{item.name}
</li>
))}
</ul>
</div>
);
};
How This Works Under the Hood
The secret lies in the .mdc header block:
globs: Tells Cursor exactly which folders and extensions to monitor.- Zero Overhead: This runs entirely inside Cursor's internal token context. It does not impact your application bundle size, production speeds, or local development environments.
Drop this file into your workspace and watch your AI coding velocity instantly double with cleaner results.
References:
To learn more about optimizing your AI workflows or to explore deeper React patterns, check out these excellent resources:
- Official Configuration Guide: Read the Cursor Rules Documentation to understand exactly how the
.mdcfile structure matches your project files. - Community Rule Marketplace: If you want inspiration for more specific development setups, browse through community-curated templates on the official Cursor Directory.
- The "Token Tax" Problem: For an in-depth breakdown of why moving to modular
.mdcfiles saves massive API costs compared to legacy.cursorrulesfiles, read the complete guide by the Vibe Coding Academy. - React Implementation Challenges: For a humorous but highly practical look at the common bugs LLMs make without proper guidance, read 10 React Rules Cursor Loves to Ignore on Medium.