|
| 1 | +--- |
| 2 | +description: 'ReactJS development standards and best practices' |
| 3 | +applyTo: '**/*.jsx, **/*.tsx, **/*.js, **/*.ts, **/*.css, **/*.scss' |
| 4 | +--- |
| 5 | + |
| 6 | +# ReactJS Development Instructions |
| 7 | + |
| 8 | +Instructions for building high-quality ReactJS applications with modern patterns, hooks, and best practices following the official React documentation at https://react.dev. |
| 9 | + |
| 10 | +## Project Context |
| 11 | +- Latest React version (React 19+) |
| 12 | +- TypeScript for type safety (when applicable) |
| 13 | +- Functional components with hooks as default |
| 14 | +- Follow React's official style guide and best practices |
| 15 | +- Use modern build tools (Vite, Create React App, or custom Webpack setup) |
| 16 | +- Implement proper component composition and reusability patterns |
| 17 | + |
| 18 | +## Development Standards |
| 19 | + |
| 20 | +### Architecture |
| 21 | +- Use functional components with hooks as the primary pattern |
| 22 | +- Implement component composition over inheritance |
| 23 | +- Organize components by feature or domain for scalability |
| 24 | +- Separate presentational and container components clearly |
| 25 | +- Use custom hooks for reusable stateful logic |
| 26 | +- Implement proper component hierarchies with clear data flow |
| 27 | + |
| 28 | +### TypeScript Integration |
| 29 | +- Use TypeScript interfaces for props, state, and component definitions |
| 30 | +- Define proper types for event handlers and refs |
| 31 | +- Implement generic components where appropriate |
| 32 | +- Use strict mode in `tsconfig.json` for type safety |
| 33 | +- Leverage React's built-in types (`React.FC`, `React.ComponentProps`, etc.) |
| 34 | +- Create union types for component variants and states |
| 35 | + |
| 36 | +### Component Design |
| 37 | +- Follow the single responsibility principle for components |
| 38 | +- Use descriptive and consistent naming conventions |
| 39 | +- Implement proper prop validation with TypeScript or PropTypes |
| 40 | +- Design components to be testable and reusable |
| 41 | +- Keep components small and focused on a single concern |
| 42 | +- Use composition patterns (render props, children as functions) |
| 43 | + |
| 44 | +### State Management |
| 45 | +- Use `useState` for local component state |
| 46 | +- Implement `useReducer` for complex state logic |
| 47 | +- Leverage `useContext` for sharing state across component trees |
| 48 | +- Consider external state management (Redux Toolkit, Zustand) for complex applications |
| 49 | +- Implement proper state normalization and data structures |
| 50 | +- Use React Query or SWR for server state management |
| 51 | + |
| 52 | +### Hooks and Effects |
| 53 | +- Use `useEffect` with proper dependency arrays to avoid infinite loops |
| 54 | +- Implement cleanup functions in effects to prevent memory leaks |
| 55 | +- Use `useMemo` and `useCallback` for performance optimization when needed |
| 56 | +- Create custom hooks for reusable stateful logic |
| 57 | +- Follow the rules of hooks (only call at the top level) |
| 58 | +- Use `useRef` for accessing DOM elements and storing mutable values |
| 59 | + |
| 60 | +### Styling |
| 61 | +- Use CSS Modules, Styled Components, or modern CSS-in-JS solutions |
| 62 | +- Implement responsive design with mobile-first approach |
| 63 | +- Follow BEM methodology or similar naming conventions for CSS classes |
| 64 | +- Use CSS custom properties (variables) for theming |
| 65 | +- Implement consistent spacing, typography, and color systems |
| 66 | +- Ensure accessibility with proper ARIA attributes and semantic HTML |
| 67 | + |
| 68 | +### Performance Optimization |
| 69 | +- Use `React.memo` for component memoization when appropriate |
| 70 | +- Implement code splitting with `React.lazy` and `Suspense` |
| 71 | +- Optimize bundle size with tree shaking and dynamic imports |
| 72 | +- Use `useMemo` and `useCallback` judiciously to prevent unnecessary re-renders |
| 73 | +- Implement virtual scrolling for large lists |
| 74 | +- Profile components with React DevTools to identify performance bottlenecks |
| 75 | + |
| 76 | +### Data Fetching |
| 77 | +- Use modern data fetching libraries (React Query, SWR, Apollo Client) |
| 78 | +- Implement proper loading, error, and success states |
| 79 | +- Handle race conditions and request cancellation |
| 80 | +- Use optimistic updates for better user experience |
| 81 | +- Implement proper caching strategies |
| 82 | +- Handle offline scenarios and network errors gracefully |
| 83 | + |
| 84 | +### Error Handling |
| 85 | +- Implement Error Boundaries for component-level error handling |
| 86 | +- Use proper error states in data fetching |
| 87 | +- Implement fallback UI for error scenarios |
| 88 | +- Log errors appropriately for debugging |
| 89 | +- Handle async errors in effects and event handlers |
| 90 | +- Provide meaningful error messages to users |
| 91 | + |
| 92 | +### Forms and Validation |
| 93 | +- Use controlled components for form inputs |
| 94 | +- Implement proper form validation with libraries like Formik, React Hook Form |
| 95 | +- Handle form submission and error states appropriately |
| 96 | +- Implement accessibility features for forms (labels, ARIA attributes) |
| 97 | +- Use debounced validation for better user experience |
| 98 | +- Handle file uploads and complex form scenarios |
| 99 | + |
| 100 | +### Routing |
| 101 | +- Use React Router for client-side routing |
| 102 | +- Implement nested routes and route protection |
| 103 | +- Handle route parameters and query strings properly |
| 104 | +- Implement lazy loading for route-based code splitting |
| 105 | +- Use proper navigation patterns and back button handling |
| 106 | +- Implement breadcrumbs and navigation state management |
| 107 | + |
| 108 | +### Testing |
| 109 | +- Write unit tests for components using React Testing Library |
| 110 | +- Test component behavior, not implementation details |
| 111 | +- Use Jest for test runner and assertion library |
| 112 | +- Implement integration tests for complex component interactions |
| 113 | +- Mock external dependencies and API calls appropriately |
| 114 | +- Test accessibility features and keyboard navigation |
| 115 | + |
| 116 | +### Security |
| 117 | +- Sanitize user inputs to prevent XSS attacks |
| 118 | +- Validate and escape data before rendering |
| 119 | +- Use HTTPS for all external API calls |
| 120 | +- Implement proper authentication and authorization patterns |
| 121 | +- Avoid storing sensitive data in localStorage or sessionStorage |
| 122 | +- Use Content Security Policy (CSP) headers |
| 123 | + |
| 124 | +### Accessibility |
| 125 | +- Use semantic HTML elements appropriately |
| 126 | +- Implement proper ARIA attributes and roles |
| 127 | +- Ensure keyboard navigation works for all interactive elements |
| 128 | +- Provide alt text for images and descriptive text for icons |
| 129 | +- Implement proper color contrast ratios |
| 130 | +- Test with screen readers and accessibility tools |
| 131 | + |
| 132 | +## Implementation Process |
| 133 | +1. Plan component architecture and data flow |
| 134 | +2. Set up project structure with proper folder organization |
| 135 | +3. Define TypeScript interfaces and types |
| 136 | +4. Implement core components with proper styling |
| 137 | +5. Add state management and data fetching logic |
| 138 | +6. Implement routing and navigation |
| 139 | +7. Add form handling and validation |
| 140 | +8. Implement error handling and loading states |
| 141 | +9. Add testing coverage for components and functionality |
| 142 | +10. Optimize performance and bundle size |
| 143 | +11. Ensure accessibility compliance |
| 144 | +12. Add documentation and code comments |
| 145 | + |
| 146 | +## Additional Guidelines |
| 147 | +- Follow React's naming conventions (PascalCase for components, camelCase for functions) |
| 148 | +- Use meaningful commit messages and maintain clean git history |
| 149 | +- Implement proper code splitting and lazy loading strategies |
| 150 | +- Document complex components and custom hooks with JSDoc |
| 151 | +- Use ESLint and Prettier for consistent code formatting |
| 152 | +- Keep dependencies up to date and audit for security vulnerabilities |
| 153 | +- Implement proper environment configuration for different deployment stages |
| 154 | +- Use React Developer Tools for debugging and performance analysis |
| 155 | + |
| 156 | +## Common Patterns |
| 157 | +- Higher-Order Components (HOCs) for cross-cutting concerns |
| 158 | +- Render props pattern for component composition |
| 159 | +- Compound components for related functionality |
| 160 | +- Provider pattern for context-based state sharing |
| 161 | +- Container/Presentational component separation |
| 162 | +- Custom hooks for reusable logic extraction |
0 commit comments