---
description: This rule explains React component patterns, hooks usage, and best practices.
globs: **/*.jsx,**/*.tsx
alwaysApply: false
---
# React rules
- Use functional components with hooks instead of class components
- Use custom hooks for reusable logic
- Use the Context API for state management when needed
- Use proper prop validation with PropTypes
- Use React.memo for performance optimization when necessary
- Use fragments to avoid unnecessary DOM elements
- Use proper list rendering with keys
- Prefer composition over inheritance
This rule serves as your React guide in Cursor, offering AI assistance with React component patterns, hooks usage, and best practices. Whether you're building new components or refactoring existing code, this rule helps you follow modern React conventions and write maintainable code.
The React rule provides guidance for writing high-quality React code according to modern best practices. It focuses on several key areas of React development:
When activated, the rule instructs Cursor's AI to provide suggestions and code completions aligned with these React best practices, helping you write more maintainable and efficient React code.
The rule encourages using functional components with hooks instead of class components, following modern React development patterns. This approach results in more concise code that's easier to test and maintain.
For example, instead of writing:
class UserProfile extends React.Component {
constructor(props) {
super(props);
this.state = { name: props.name };
}
render() {
return <div>Hello, {this.state.name}!</div>;
}
}
The AI will suggest functional components with hooks:
function UserProfile({ name }) {
const [userName, setUserName] = useState(name);
return <div>Hello, {userName}!</div>;
}
The rule promotes extracting reusable logic into custom hooks, making your code more modular and testable. Custom hooks allow you to share stateful logic between components without duplicating code.
When global state is needed, the rule suggests using React's Context API appropriately, helping you avoid prop drilling while maintaining clean component architecture.
The rule also covers:
To benefit from these React best practices while working in Cursor:
The rule is defined in the file react.mdc
within your project's .cursor/rules
directory.
This rule is likely configured as an "Auto Attached" rule, as it has glob patterns (**/*.jsx,**/*.tsx
). This means the rule activates automatically whenever you're working with JSX or TSX files in your project.
If you need to manually invoke the rule in other contexts, you can type @react
in the chat or Cmd-K interface.
When activated, the rule provides context to Cursor's AI, guiding it to offer suggestions and code completions that follow modern React best practices.
When creating new components, let Cursor help you structure them according to best practices. The AI will suggest functional components with hooks and proper prop handling.
If you're working with older React codebases that use class components, the AI can help you refactor to functional components with hooks, making your code more maintainable.
When you're implementing state management in your components, the AI will provide guidance on whether to use local state with useState, shared logic with custom hooks, or global state with Context API based on your specific needs.
As your application grows, the AI can help identify opportunities for performance optimization using React.memo, useMemo, and useCallback in appropriate situations.