machichdigital
RegelCursor RulesLizenz: CC0 1.0frei kopierbar

React

Basis-Cursor-Regel für React: Function Components, Hooks und Props im modernen React-Stil.

⬇ Als Datei laden

× kopiert× heruntergeladenBewertung:

Basis-Cursor-Regel für React: Function Components, Hooks und Props im modernen React-Stil.

Original-Beschreibung der Autoren: React best practices and patterns for modern web applications

Die Regel

---
description: "React best practices and patterns for modern web applications"
globs: **/*.tsx, **/*.jsx, components/**/*
alwaysApply: false
---
# React Best Practices

## Component Structure
- Use functional components over class components
- Keep components small and focused
- Extract reusable logic into custom hooks
- Use composition over inheritance
- Implement proper prop types with TypeScript
- Split large components into smaller, focused ones

## Hooks
- Follow the Rules of Hooks
- Use custom hooks for reusable logic
- Keep hooks focused and simple
- Use appropriate dependency arrays in useEffect
- Implement cleanup in useEffect when needed
- Avoid nested hooks

## State Management
- Use useState for local component state
- Implement useReducer for complex state logic
- Use Context API for shared state
- Keep state as close to where it's used as possible
- Avoid prop drilling through proper state management
- Use state management libraries only when necessary

## Performance
- Implement proper memoization (useMemo, useCallback)
- Use React.memo for expensive components
- Avoid unnecessary re-renders
- Implement proper lazy loading
- Use proper key props in lists
- Profile and optimize render performance

## Forms
- Use controlled components for form inputs
- Implement proper form validation
- Handle form submission states properly
- Show appropriate loading and error states
- Use form libraries for complex forms
- Implement proper accessibility for forms

## Error Handling
- Implement Error Boundaries
- Handle async errors properly
- Show user-friendly error messages
- Implement proper fallback UI
- Log errors appropriately
- Handle edge cases gracefully

## Testing
- Write unit tests for components
- Implement integration tests for complex flows
- Use React Testing Library
- Test user interactions
- Test error scenarios
- Implement proper mock data

## Accessibility
- Use semantic HTML elements
- Implement proper ARIA attributes
- Ensure keyboard navigation
- Test with screen readers
- Handle focus management
- Provide proper alt text for images

## Code Organization
- Group related components together
- Use proper file naming conventions
- Implement proper directory structure
- Keep styles close to components
- Use proper imports/exports
- Document complex component logic

So nutzt du sie

Die Regel kopieren (Button oben) oder als Datei herunterladen und im Projekt unter .cursor/rules/ ablegen — Cursor lädt sie beim nächsten Start automatisch. Ältere Cursor-Versionen lesen alternativ eine einzelne .cursorrules-Datei im Projektstamm; dort einfach den Regel-Text ohne den Kopfblock zwischen den ----Zeilen einfügen.

Der Regel-Text ist englisch — Cursor versteht ihn unabhängig von der Sprache, in der Sie mit dem Editor chatten.

Im Detail

Allgemeine Cursor-Regel für React-Projekte: definiert Konventionen für Function Components, Hooks-Nutzung, Props-Typisierung und Dateistruktur, damit generierter Code dem üblichen React-Stil (statt Klassen-Komponenten oder veralteten Patterns) folgt. Eher ein Grundgerüst als spezialisierte Varianten wie Chakra- oder GraphQL-Regeln — sinnvoll als Basis-Regel für Standard-React-Projekte ohne bestimmtes UI- oder State-Framework. Wer bereits eine spezifischere Stack-Regel nutzt (z. B. für Chakra UI oder Apollo), sollte diese allgemeine Regel eher ergänzend als exklusiv einsetzen, um Redundanzen zu vermeiden.

Praxis-Tipp

Als Basis-Regel in .cursor/rules/react.mdc verwenden und bei Bedarf mit einer spezifischeren Regel (z. B. Chakra oder Mobx) kombinieren statt sie zu ersetzen.

Lizenz & Quelle

Inhalt ansehen (react.mdc)
Lade …

Erfahrungen & Kommentare.

Funktioniert der Regel bei Ihnen? Tipps, Stolperfallen, Varianten — teilen Sie es mit der Community.

Lade Kommentare …

Ihre IP-Adresse wird zum Schutz vor Missbrauch gespeichert und nach 14 Tagen automatisch entfernt (Datenschutz).

Passt dazu.