--- description: Guidelines and best practices for developing Lightning Web Components (LWC) on Salesforce Platform. applyTo: 'force-app/main/default/lwc/**' --- # LWC Development ## General Instructions - Each LWC should reside in its own folder under `force-app/main/default/lwc/`. - The folder name should match the component name (e.g., `myComponent` for `myComponent`). - Each component folder should contain the following files: - `myComponent.html`: The HTML template file. - `myComponent.js`: The JavaScript controller file. - `myComponent.js-meta.xml`: The metadata configuration file. - Optional: `myComponent.css` for component-specific styles. - Optional: `myComponent.test.js` for Jest unit tests. ## Development Best Practices ### Component Design - **Single Responsibility**: Each component should have a single, well-defined purpose. - **Reusability**: Design components to be reusable across different parts of the application. - **Composition**: Prefer composing smaller components into larger ones rather than creating monolithic components. - **Public API**: Use `@api` decorators to expose properties and methods that need to be accessed by parent components. - **Private State**: Use `@track` for reactive properties that are internal to the component. ### Performance Optimization - **Lazy Loading**: Load components only when needed to improve initial load times. - **Efficient Rendering**: Minimize DOM updates by batching changes and using conditional rendering. - **Avoid Unnecessary Re-renders**: Use getters and setters wisely to prevent unnecessary re-renders. - **Use Lightning Data Service (LDS)**: Prefer LDS for data access to leverage built-in caching and performance optimizations. ### Security Considerations - **Sanitize Inputs**: Always sanitize user inputs to prevent XSS attacks. - **Use `@wire`**: Prefer using `@wire` for data access to ensure security and compliance with Salesforce's security model. - **Avoid Direct DOM Manipulation**: Use LWC's reactive properties and templates instead of manipulating the DOM directly. # Lightning Web Component (LWC) Development Best Practices ## Core Principles ### 1. Use Lightning Components Over HTML Tags Always prefer Lightning Web Component library components over plain HTML elements for consistency, accessibility, and future-proofing. #### ✅ Recommended Approach ```html ``` #### ❌ Avoid Plain HTML ```html ``` ### 2. Lightning Component Mapping Guide | HTML Element | Lightning Component | Key Attributes | |--------------|-------------------|----------------| | `