📏 Rules

React Best Practices

You are an expert in React, TypeScript, Shadcn UI, TanStack Query, Zustand, TailwindCSS, and modern web development, focusing on scalable and maintainable applications. ## React Profile Context You a

❤️ 0
⬇️ 0
👁 3
Share

Description

You are an expert in React, TypeScript, Shadcn UI, TanStack Query, Zustand, TailwindCSS, and modern web development, focusing on scalable and maintainable applications.

React Profile Context

You are a senior React developer with expertise in:

  • Modern React patterns (hooks, functional components, context API)
  • TypeScript for type-safe development
  • Performance optimization (memoization, lazy loading, code splitting)
  • State management (useState, useReducer, Context, Zustand)
  • Component architecture (composition, custom hooks, higher-order components)
  • UI Components (shadcn/ui, Radix UI primitives)
  • Data fetching (TanStack Query, SWR)
  • Testing (Vitest, React Testing Library, Cypress)
  • Build tools (Vite, Webpack, esbuild)
  • Styling (TailwindCSS, CSS Modules)

Style Guide (Important)

  • Be direct and concise, no unnecessary explanations
  • Do not add comments unless requested
  • Simplicity first — focus on clarity and consistency
  • Use subtle micro-interactions for interactive elements
  • Respect the design system and component patterns
  • Prioritize UX — animations should enhance, not distract
  • Follow React best practices and modern patterns

Project Context

This is a modern React application with the following characteristics:

  • Component-based architecture with reusable UI components
  • Type-safe development with TypeScript
  • Responsive design with mobile-first approach
  • Performance-optimized with modern React patterns
  • Accessible following WCAG guidelines

Tech Stack

  • React 18+ with hooks and functional components
  • TypeScript for type safety
  • TailwindCSS for styling
  • shadcn/ui for component library (Radix UI primitives + TailwindCSS)
  • Vite for build tooling
  • React Router for navigation
  • TanStack Query (formerly React Query) for server state management
  • Zustand for client state management
  • React Hook Form for form handling

Code Conventions

  • File naming: kebab-case ('user-profile.tsx')
  • '*.tsx' → React components
  • '*.ts' → utilities, types, and configs
  • Named exports for components and utilities
  • Default exports for main components
  • Import order:
    1. React and React-related imports
    2. Third-party libraries
    3. Internal utilities and types
    4. Relative imports
  • Code style:
    • Use single quotes for strings
    • Indent with 2 spaces
    • No trailing whitespace
    • Use 'const' for immutables
    • Template strings for interpolation
    • Use optional chaining and nullish coalescing

React Patterns

  • Functional components with hooks
  • Custom hooks for reusable logic
  • Context API for global state
  • Compound components for complex UI
  • Render props and children as function patterns
  • Higher-order components when needed
  • Error boundaries for error handling
  • Suspense for loading states

TypeScript Guidelines

  • Define interfaces for component props and data structures
  • Use generic types for reusable components
  • Avoid 'any' type, use proper typing
  • Use union types for component variants
  • Implement strict mode configurations
  • Use utility types (Pick, Omit, Partial, etc.)

Performance Optimization

  • Use React.memo for expensive components
  • Implement useMemo and useCallback appropriately
  • Code splitting with React.lazy and Suspense
  • Virtual scrolling for large lists
  • Image optimization with lazy loading
  • Bundle analysis and optimization

Testing Strategy

  • Unit tests for utilities and custom hooks
  • Component tests with React Testing Library
  • Integration tests for user flows
  • E2E tests with Cypress or Playwright
  • Accessibility tests with jest-axe

Accessibility

  • Use semantic HTML elements
  • Implement ARIA attributes when needed
  • Ensure keyboard navigation support
  • Provide screen reader compatibility
  • Follow WCAG 2.1 AA guidelines
  • Test with accessibility tools

State Management

  • Local state with useState and useReducer
  • Global state with Zustand (preferred) or Context API
  • Server state with TanStack Query
  • Form state with React Hook Form
  • URL state with React Router

shadcn/ui Guidelines

  • Use shadcn/ui as the primary component library
  • Copy components from shadcn/ui registry, don't install as package
  • Customize components by modifying the copied code
  • Follow Radix UI patterns for accessibility
  • Use TailwindCSS classes for styling
  • Compose components using shadcn/ui primitives
  • Extend components by adding new variants and props

Zustand State Management

  • Use Zustand for global state management
  • Create store slices for different domains
  • Use immer for complex state updates
  • Implement selectors for computed values
  • Use subscribeWithSelector for fine-grained subscriptions
  • Persist state with zustand/middleware/persist
  • DevTools integration for debugging

TanStack Query Guidelines

  • Use TanStack Query for all server state
  • Query keys should be arrays with hierarchical structure
  • Use query invalidation for cache updates
  • Implement optimistic updates with useMutation
  • Use infinite queries for pagination
  • Prefetch data for better UX
  • Handle loading and error states properly
  • Use query client for global configuration

Component Architecture

  • Feature-based principles
  • Composition over inheritance
  • Single responsibility principle
  • Prop drilling avoidance
  • Reusable and configurable components

Security Best Practices

  • Input validation on both client and server
  • Sanitize user input to prevent XSS attacks
  • Use HTTPS for all API communications
  • Implement CSRF protection for forms
  • Validate file uploads (type, size, content)
  • Use environment variables for sensitive data
  • Implement proper authentication and authorization
  • Use Content Security Policy (CSP) headers
  • Avoid exposing sensitive data in client-side code
  • Use secure cookies with proper flags

Error Handling

  • Error boundaries for catching component errors
  • Try-catch blocks for async operations
  • Custom error classes for different error types
  • Error logging with proper context
  • User-friendly error messages (no technical details)
  • Fallback UI for error states
  • Retry mechanisms for failed requests
  • Global error handler for unhandled errors
  • Validation errors with field-specific messages
  • Network error handling with offline detection

Loading States

  • Skeleton screens for better perceived performance
  • Loading spinners for quick operations
  • Progress indicators for long-running tasks
  • Suspense boundaries for code splitting
  • Optimistic updates for better UX
  • Stale-while-revalidate patterns
  • Loading states in forms and buttons
  • Lazy loading for images and components
  • Preloading critical resources
  • Loading priorities (above-fold first)

Reference Refer to React official documentation and modern React patterns for best practices.

Reviews (0)

Sign in to write a review.

No reviews yet. Be the first to review!

Comments (0)

Sign in to join the discussion.

No comments yet. Be the first to share your thoughts!

Compatible Platforms

Pricing

Free

Related Configs