Add comprehensive MDC rule files for various technologies

- Add 150+ MDC rule files covering a wide range of programming languages, frameworks, and tools
- Include best practices, coding standards, and guidelines for technologies like Python, JavaScript, React, AWS, CI/CD, and more
- Implement consistent formatting and structure across rule files
- Add concluding sections with recommendations for maintainability, security, and performance
- Enhance project documentation with comprehensive technology-specific guidelines
This commit is contained in:
sanjeed5
2025-02-28 19:31:47 +05:30
parent 69ee481328
commit d96d9ab8c8
159 changed files with 40387 additions and 309 deletions
+266 -149
View File
@@ -1,167 +1,284 @@
---
description: Enforces best practices for React development, covering code structure, performance, security, and testing to ensure maintainable and efficient applications.
description: Comprehensive guide to React best practices, covering code organization, performance, security, testing, and common pitfalls. Adhering to these guidelines helps developers build maintainable, scalable, and high-performing React applications.
globs: **/*.{js,jsx,ts,tsx}
---
- **Use Functional Components with Hooks:**
- Favor functional components over class components for simplicity and better performance.
- Leverage React Hooks (useState, useEffect, useContext, useReducer, useCallback, useMemo, useRef, useImperativeHandle, useLayoutEffect, useDebugValue) for managing state, side effects, and context.
# React Best Practices: A Comprehensive Guide
- **Component Composition:**
- Build complex UIs by composing smaller, reusable components.
- Use props to pass data down the component tree and callbacks to handle events.
This document outlines the best practices for developing React applications, covering various aspects from code organization to security and testing. Following these guidelines leads to more maintainable, scalable, and performant applications.
- **Meaningful Component Names:**
- Give component names that accurately reflect their functionality.
- Follow a consistent naming convention (e.g., PascalCase for components, camelCase for props).
## 1. Code Organization and Structure
- **Keep Components Small and Focused:**
- Adhere to the Single Responsibility Principle; each component should have a clear, focused purpose.
- Break down large components into smaller, more manageable pieces.
### 1.1 Directory Structure
- **Destructuring Props:**
- Use destructuring to access props within functional components for improved readability.
A well-defined directory structure is crucial for maintainability. Here's a recommended structure:
- **Avoid Inline Styles:**
- Use CSS modules, styled-components, or a CSS-in-JS library for managing styles instead of inline styles.
- Keep styles separate from component logic for better maintainability.
- **Use Arrow Functions:**
- Use arrow functions for concise syntax and lexical `this` binding.
- Be mindful of performance implications when passing arrow functions directly as props; consider using `useCallback`.
src/
├── components/
│ ├── Button/
│ │ ├── Button.jsx
│ │ ├── Button.module.css
│ │ └── Button.test.jsx
│ ├── Input/
│ │ ├── Input.jsx
│ │ ├── Input.module.css
│ │ └── Input.test.jsx
│ └── ...
├── contexts/
│ ├── AuthContext.jsx
│ └── ThemeContext.jsx
├── hooks/
│ ├── useAuth.js
│ └── useTheme.js
├── pages/
│ ├── Home.jsx
│ ├── About.jsx
│ └── ...
├── services/
│ ├── api.js
│ └── auth.js
├── utils/
│ ├── helpers.js
│ └── validators.js
├── App.jsx
├── index.jsx
└── ...
- **PropTypes for Type Checking (with alternatives):**
- Use PropTypes (though consider TypeScript) to define the expected types of props passed to components.
- Enforce type checking at runtime to catch type-related errors early.
- Strongly consider TypeScript for static type checking for enhanced code quality and maintainability.
- **Immutability:**
- Treat state as immutable; avoid directly modifying state objects.
- Use `setState`, the spread operator (`...`), or immutable data structures (e.g., Immutable.js) to update state.
- **`components/`**: Reusable UI components.
- Each component has its own directory containing the component file, associated styles (using CSS modules), and tests.
- **`contexts/`**: React context providers.
- **`hooks/`**: Custom React hooks.
- **`pages/`**: Top-level components representing different routes or views.
- **`services/`**: API interaction logic.
- **`utils/`**: Utility functions.
- **Code Organization and Structure:**
- **Directory Structure:**
- Organize components into folders based on features or modules.
- Use a `components` directory for reusable UI elements.
- Consider a `hooks` directory for custom hooks.
- Separate API services into a `services` or `api` directory.
- **File Naming Conventions:**
- Use `.jsx` or `.tsx` extensions for React components.
- Name files according to the component they export (e.g., `MyComponent.jsx`).
- Use `index.js` or `index.ts` files to export multiple components from a directory.
- **Module Organization:**
- Use ES modules (`import` and `export`) for modular code.
- Group related components and logic into modules.
- **Component Architecture:**
- **Atomic Design:** Consider using Atomic Design principles (Atoms, Molecules, Organisms, Templates, Pages) to structure your components.
- **Presentational and Container Components:** Separate presentational (UI-focused) and container (logic-focused) components.
- **Code Splitting:**
- Use React.lazy and Suspense to lazy-load components and reduce initial bundle size.
- Implement route-based code splitting for larger applications.
- Consider using dynamic imports for splitting non-route components as well.
### 1.2 File Naming Conventions
- **Common Patterns and Anti-patterns:**
- **Design Patterns:**
- **Higher-Order Components (HOCs):** Use HOCs to share logic between components.
- **Render Props:** Use render props for more flexible code reuse.
- **Compound Components:** Create components that implicitly share state and behavior with their children (e.g., `<Tabs>`, `<Tab>`).
- **Provider Pattern:** Use the Context API to provide data to a subtree of components.
- **Recommended Approaches:**
- Use controlled components for forms to manage input values in React state.
- Use keys when rendering lists of elements for efficient updates.
- Use memoization techniques (React.memo, useMemo, useCallback) to prevent unnecessary re-renders.
- **Anti-patterns:**
- **Direct DOM Manipulation:** Avoid directly manipulating the DOM; use React's virtual DOM instead.
- **Mutating State Directly:** Never directly modify state; use `setState` or the appropriate hook update function.
- **Over-reliance on Global State:** Avoid storing too much state in global stores; consider component-level state or context when appropriate.
- **State Management:**
- **Component State:** Use `useState` for simple component-level state.
- **Context API:** Use the Context API for sharing state across a subtree of components.
- **Redux/Zustand/Recoil/MobX:** Consider using a state management library (Redux, Zustand, Recoil, MobX) for complex application state.
- **Error Handling:**
- **Error Boundaries:** Use Error Boundaries to catch errors during rendering and prevent the entire application from crashing.
- **Try/Catch Blocks:** Use try/catch blocks for handling errors in event handlers and lifecycle methods.
- **Components**: Use PascalCase (e.g., `MyComponent.jsx`).
- **Hooks**: Use camelCase prefixed with `use` (e.g., `useMyHook.js`).
- **Contexts**: Use PascalCase suffixed with `Context` (e.g., `MyContext.jsx`).
- **Services/Utils**: Use camelCase (e.g., `apiService.js`, `stringUtils.js`).
- **CSS Modules**: Use `.module.css` or `.module.scss` (e.g., `Button.module.css`).
- **Performance Considerations:**
- **Memoization:**
- Use `React.memo` to memoize functional components and prevent unnecessary re-renders.
- Use `useMemo` to memoize computed values and prevent unnecessary recalculations.
- Use `useCallback` to memoize event handler functions and prevent unnecessary re-renders of child components.
- **Virtualization:**
- Use virtualization libraries (e.g., `react-window`, `react-virtualized`) for rendering large lists of data efficiently.
- **Debouncing and Throttling:**
- Use debouncing and throttling techniques to limit the frequency of function calls in response to user input.
- **Image Optimization:**
- Optimize images for the web to reduce file sizes and improve loading times.
- Use lazy loading for images that are not immediately visible.
- **Bundle Size Optimization:**
- Use code splitting to reduce the initial bundle size.
- Remove unused code with tree shaking.
- Compress code with minification.
- **Rendering Optimization:**
- Use `shouldComponentUpdate` (in class components) or `React.memo` (in functional components) to prevent unnecessary re-renders.
- Implement pure components (components that only re-render when their props change) for performance gains.
- **Lazy Loading:**
- Use `React.lazy` and `Suspense` to load components on demand and improve initial load time.
### 1.3 Module Organization
- **Security Best Practices:**
- **XSS Prevention:**
- Sanitize user input to prevent cross-site scripting (XSS) attacks.
- Use React's built-in escaping mechanisms to render data safely.
- **CSRF Protection:**
- Implement cross-site request forgery (CSRF) protection to prevent unauthorized requests.
- Use anti-CSRF tokens to verify requests.
- **Authentication and Authorization:**
- Use secure authentication and authorization patterns to protect sensitive data and functionality.
- Implement strong password policies.
- Use multi-factor authentication.
- **Data Protection:**
- Encrypt sensitive data at rest and in transit.
- Use HTTPS to secure communication between the client and server.
- **Secure API Communication:**
- Validate API responses to prevent unexpected data from compromising the application.
- **Co-location**: Keep related files (component, styles, tests) together in the same directory.
- **Single Responsibility**: Each module should have a clear and specific purpose.
- **Avoid Circular Dependencies**: Ensure modules don't depend on each other in a circular manner.
- **Testing Approaches:**
- **Unit Testing:**
- Write unit tests to verify the behavior of individual components and functions.
- Use testing libraries like Jest and React Testing Library.
- **Integration Testing:**
- Write integration tests to verify the interaction between multiple components and modules.
- **End-to-End Testing:**
- Write end-to-end tests to verify the entire application flow.
- Use testing frameworks like Cypress or Playwright.
- **Test Organization:**
- Organize tests into a `__tests__` directory alongside the component files.
- Follow a consistent naming convention for test files (e.g., `MyComponent.test.jsx`).
- **Mocking and Stubbing:**
- Use mocking and stubbing techniques to isolate components during testing.
- Mock external dependencies like API calls and browser APIs.
### 1.4 Component Architecture
- **Common Pitfalls and Gotchas:**
- **Incorrect `this` Binding:**
- Be mindful of `this` binding in class components; use arrow functions or `bind` to ensure the correct context.
- **Missing Keys in Lists:**
- Provide unique keys to elements when rendering lists to prevent rendering issues.
- **Asynchronous State Updates:**
- Understand how `setState` and hook update functions handle asynchronous updates; use functional updates when necessary.
- **Memory Leaks:**
- Clean up side effects in `useEffect` hooks to prevent memory leaks.
- **Version-Specific Issues:**
- Be aware of compatibility concerns when upgrading React versions.
- Consult the React documentation and release notes for migration guides.
- **Atomic Design**: Consider using Atomic Design principles (Atoms, Molecules, Organisms, Templates, Pages) to structure components.
- **Composition over Inheritance**: Favor component composition to reuse code and functionality.
- **Presentational and Container Components**: Separate UI rendering (presentational) from state management and logic (container).
- **Tooling and Environment:**
- **Recommended Development Tools:**
- VS Code with React-specific extensions (e.g., ESLint, Prettier, Babel).
- Chrome DevTools for debugging.
- React Developer Tools extension for inspecting component hierarchy and state.
- **Build Configuration:**
- Use tools like webpack, Parcel, or Rollup for bundling and optimizing code.
- Configure Babel to transpile JSX and modern JavaScript syntax.
- **Linting and Formatting:**
- Use ESLint with React-specific rules to enforce code style and best practices.
- Use Prettier to automatically format code.
- **Deployment Best Practices:**
- Use a CDN to serve static assets.
- Use server-side rendering (SSR) or static site generation (SSG) for improved performance and SEO.
- **CI/CD Integration:**
- Integrate CI/CD pipelines for automated testing, building, and deployment.
### 1.5 Code Splitting Strategies
- **Route-Based Splitting**: Use `React.lazy` and `Suspense` to load components only when a specific route is accessed. This is very common and improves initial load time.
- **Component-Based Splitting**: Split large components into smaller chunks that can be loaded on demand.
- **Bundle Analyzer**: Use a tool like `webpack-bundle-analyzer` to identify large dependencies and optimize bundle size.
## 2. Common Patterns and Anti-patterns
### 2.1 Design Patterns
- **Higher-Order Components (HOCs)**: Reusable logic that wraps components (use with caution; prefer hooks).
- **Render Props**: Sharing code using a prop whose value is a function.
- **Compound Components**: Components that work together implicitly (e.g., `Tabs`, `Tab`).
- **Hooks**: Reusable stateful logic that can be shared across functional components.
### 2.2 Recommended Approaches
- **Form Handling**: Use controlled components with local state or a form library like Formik or React Hook Form.
- **API Calls**: Use `useEffect` hook to make API calls and manage loading states.
- **Conditional Rendering**: Use short-circuit evaluation (`&&`) or ternary operators for simple conditions; use separate components for complex scenarios.
- **List Rendering**: Always provide a unique and stable `key` prop when rendering lists.
### 2.3 Anti-patterns and Code Smells
- **Direct DOM Manipulation**: Avoid directly manipulating the DOM; let React handle updates.
- **Mutating State Directly**: Always use `setState` or the state updater function to modify state.
- **Inline Styles**: Use CSS modules or styled-components for maintainable styles.
- **Over-Engineering**: Avoid using complex solutions for simple problems.
- **Prop Drilling**: Passing props through multiple levels of components without them being used.
### 2.4 State Management Best Practices
- **Local State**: Use `useState` for component-specific state.
- **Context API**: Use `useContext` for global state accessible to many components, but avoid for very frequently updated data.
- **Redux/Mobx**: Use these libraries for complex state management in large applications.
- **Recoil/Zustand**: Lightweight alternatives to Redux, often easier to set up and use.
- **Immutable Data**: Treat state as immutable to prevent unexpected side effects.
### 2.5 Error Handling Patterns
- **Error Boundaries**: Wrap components with error boundaries to catch errors during rendering and prevent crashes.
- **Try-Catch Blocks**: Use try-catch blocks for handling errors in asynchronous operations and event handlers.
- **Centralized Error Logging**: Implement a centralized error logging service to track errors and improve application stability.
## 3. Performance Considerations
### 3.1 Optimization Techniques
- **Memoization**: Use `React.memo`, `useMemo`, and `useCallback` to prevent unnecessary re-renders and recalculations.
- **Virtualization**: Use libraries like `react-window` or `react-virtualized` to efficiently render large lists or tables.
- **Debouncing/Throttling**: Limit the rate at which functions are executed (e.g., in input fields).
- **Code Splitting**: Load code on demand using `React.lazy` and `Suspense`.
### 3.2 Memory Management
- **Avoid Memory Leaks**: Clean up event listeners, timers, and subscriptions in `useEffect`'s cleanup function.
- **Release Unused Objects**: Avoid holding onto large objects in memory when they are no longer needed.
- **Garbage Collection**: Understand how JavaScript's garbage collection works and avoid creating unnecessary objects.
### 3.3 Rendering Optimization
- **Minimize State Updates**: Avoid unnecessary state updates that trigger re-renders.
- **Batch Updates**: Batch multiple state updates into a single update using `ReactDOM.unstable_batchedUpdates`.
- **Keys**: Ensure that keys are unique and consistent across renders.
### 3.4 Bundle Size Optimization
- **Tree Shaking**: Remove unused code during the build process.
- **Minification**: Reduce the size of JavaScript and CSS files.
- **Image Optimization**: Compress and optimize images to reduce file size.
- **Dependency Analysis**: Use tools like `webpack-bundle-analyzer` to identify large dependencies.
### 3.5 Lazy Loading Strategies
- **Route-Based Lazy Loading**: Load components when a user navigates to a specific route.
- **Component-Based Lazy Loading**: Load components when they are about to be rendered.
- **Intersection Observer**: Load components when they become visible in the viewport.
## 4. Security Best Practices
### 4.1 Common Vulnerabilities and Prevention
- **Cross-Site Scripting (XSS)**: Sanitize user input to prevent malicious code injection.
- **Cross-Site Request Forgery (CSRF)**: Use anti-CSRF tokens to protect against unauthorized requests.
- **Denial of Service (DoS)**: Implement rate limiting and request validation to prevent abuse.
- **Injection Attacks**: Avoid directly embedding user input into database queries or system commands.
### 4.2 Input Validation
- **Client-Side Validation**: Validate user input in the browser to provide immediate feedback.
- **Server-Side Validation**: Always validate user input on the server to prevent malicious data.
- **Sanitize Input**: Sanitize user input to remove potentially harmful characters or code.
### 4.3 Authentication and Authorization
- **Secure Authentication**: Use secure authentication mechanisms like OAuth 2.0 or JWT.
- **Role-Based Access Control (RBAC)**: Implement RBAC to control access to resources based on user roles.
- **Multi-Factor Authentication (MFA)**: Enable MFA to add an extra layer of security.
### 4.4 Data Protection Strategies
- **Encryption**: Encrypt sensitive data at rest and in transit.
- **Data Masking**: Mask sensitive data in logs and UI displays.
- **Regular Backups**: Create regular backups of application data.
### 4.5 Secure API Communication
- **HTTPS**: Use HTTPS to encrypt communication between the client and the server.
- **API Keys**: Protect API keys and secrets.
- **CORS**: Configure Cross-Origin Resource Sharing (CORS) to prevent unauthorized access to APIs.
## 5. Testing Approaches
### 5.1 Unit Testing
- **Test Components**: Test individual components in isolation.
- **Testing Library**: Use React Testing Library for UI testing, focusing on user behavior.
- **Jest**: Use Jest as the test runner.
### 5.2 Integration Testing
- **Test Component Interactions**: Test how components interact with each other.
- **Mock API Calls**: Mock API calls to test component behavior in different scenarios.
- **React Testing Library**: Effective for testing integration points in components.
### 5.3 End-to-End (E2E) Testing
- **Test Full Application Flows**: Test complete user flows, such as login, registration, and checkout.
- **Cypress/Playwright**: Use tools like Cypress or Playwright for E2E testing.
- **Automated Browser Tests**: Automate browser tests to ensure application stability.
### 5.4 Test Organization
- **Co-locate Tests**: Keep test files close to the components they test (e.g., `Button.test.jsx` in the `Button` directory).
- **Descriptive Names**: Use descriptive names for test files and test cases.
- **Test Suites**: Organize tests into logical suites.
### 5.5 Mocking and Stubbing
- **Mock Modules**: Mock external modules or API calls to isolate components during testing.
- **Stub Functions**: Stub function implementations to control component behavior.
- **Jest Mocks**: Utilize Jest's mocking capabilities for effective unit testing.
## 6. Common Pitfalls and Gotchas
### 6.1 Frequent Mistakes
- **Ignoring Keys in Lists**: Forgetting to provide unique and stable `key` props when rendering lists.
- **Incorrect State Updates**: Mutating state directly instead of using `setState` or the state updater function.
- **Missing Dependencies in `useEffect`**: Not including all dependencies in the dependency array of the `useEffect` hook.
- **Over-Using State**: Storing derived data in state instead of calculating it on demand.
### 6.2 Edge Cases
- **Asynchronous State Updates**: Handling state updates in asynchronous operations.
- **Race Conditions**: Preventing race conditions when making multiple API calls.
- **Handling Errors in Event Handlers**: Properly handling errors in event handlers to prevent crashes.
### 6.3 Version-Specific Issues
- **React 16 vs. React 17/18**: Understanding differences in lifecycle methods, error handling, and concurrent mode.
- **Deprecated Features**: Being aware of deprecated features and using recommended alternatives.
### 6.4 Compatibility Concerns
- **Browser Compatibility**: Ensuring compatibility with different browsers and devices.
- **Library Compatibility**: Ensuring compatibility between React and other libraries.
### 6.5 Debugging Strategies
- **React DevTools**: Use React DevTools to inspect component hierarchies, props, and state.
- **Console Logging**: Use console logging to debug code and track variables.
- **Breakpoints**: Set breakpoints in the code to step through execution and inspect variables.
## 7. Tooling and Environment
### 7.1 Recommended Development Tools
- **VS Code**: A popular code editor with excellent React support.
- **Create React App**: A tool for quickly setting up a new React project.
- **React DevTools**: A browser extension for inspecting React components.
- **ESLint**: A linter for enforcing code style and preventing errors.
- **Prettier**: A code formatter for automatically formatting code.
### 7.2 Build Configuration
- **Webpack/Vite**: Configure Webpack or Vite to bundle and optimize code.
- **Babel**: Configure Babel to transpile JavaScript code to older versions.
- **Environment Variables**: Use environment variables to configure different environments.
### 7.3 Linting and Formatting
- **ESLint**: Configure ESLint with recommended React rules.
- **Prettier**: Configure Prettier to automatically format code.
- **Husky/lint-staged**: Use Husky and lint-staged to run linters and formatters before committing code.
### 7.4 Deployment Best Practices
- **Static Hosting**: Host static assets on a CDN.
- **Server-Side Rendering (SSR)**: Use SSR to improve SEO and initial load time.
- **Continuous Deployment**: Automate the deployment process using CI/CD.
### 7.5 CI/CD Integration
- **GitHub Actions/GitLab CI**: Use GitHub Actions or GitLab CI to automate testing, linting, and deployment.
- **Automated Testing**: Run automated tests on every commit or pull request.
- **Automated Deployment**: Automatically deploy code to production after successful tests.
By following these best practices, React developers can build high-quality, maintainable, and scalable applications that meet the demands of modern web development. Continual education and adaptation to emerging trends in the React ecosystem are crucial for sustained success.