Skip to main content
This guide compiles best practices for Lightning Web Components development, based on real examples from the LWC Recipes codebase.

Component Architecture

1. Keep Components Focused and Single-Purpose

Each component should have a clear, single responsibility. The recipes demonstrate this principle:
  • hello - Simple greeting display
  • contactList - Displays a list of contacts
  • contactListItem - Renders individual contact items
Breaking functionality into smaller components improves reusability and testability.

2. Use Composition Over Inheritance

Lightning Web Components favor composition. Build complex UIs by combining smaller components:
Example from the recipes: compositionContactSearch combines contactList and search functionality.

3. Leverage Component Lifecycle Hooks

Use lifecycle hooks appropriately:
  • constructor() - Initialize component state
  • connectedCallback() - Set up subscriptions, fetch data
  • disconnectedCallback() - Clean up subscriptions, timers
  • renderedCallback() - DOM manipulation (use sparingly)

Data Management

1. Use @wire for Reactive Data Fetching

The @wire decorator provides reactive, cacheable data access:
Benefits:
  • Automatic caching
  • Reactive to parameter changes
  • Built-in error handling

2. Implement Debouncing for User Input

When making server calls based on user input, always debounce to reduce unnecessary requests:

3. Use Lightning Data Service for Standard Objects

Leverage LDS for CRUD operations on standard and custom objects:
Benefits:
  • Automatic caching and cache invalidation
  • Respect field-level security
  • Optimized for performance

Event Handling

1. Use Custom Events for Parent-Child Communication

Dispatch custom events from child components:

2. Bubble Events for Deep Component Trees

For events that need to traverse multiple component levels, use bubbling:

3. Name Events Consistently

Use consistent naming conventions:
  • Prefix with action verb: select, delete, update
  • Use lowercase
  • Be descriptive: contactselect rather than select

Error Handling

1. Create Reusable Error Utilities

Centralize error processing logic:

2. Display User-Friendly Error Messages

Always show meaningful error messages using toast notifications:

3. Use Error Panels for Complex Error States

Create dedicated error panel components for consistent error display:

Styling

1. Use SLDS (Salesforce Lightning Design System)

Leverage SLDS classes for consistent styling:

2. Scope Styles with :host

Use CSS custom properties and :host for scoped styling:

3. Implement Styling Hooks for Customization

Expose CSS custom properties for external customization:

Performance Optimization

1. Use Getters for Computed Properties

Implement computed properties with getters instead of tracking reactive properties:

2. Load Third-Party Libraries Once

Use static resources and load scripts in renderedCallback with a guard:

3. Minimize DOM Queries

Cache DOM queries instead of repeating querySelector calls:

Testing

1. Test Every Component

Maintain high test coverage with unit tests for all components:

2. Mock External Dependencies

Always mock Apex methods, LDS, and platform services:

3. Include Accessibility Tests

Test accessibility for every component:

Code Quality

1. Use ESLint and Prettier

Maintain consistent code style with automated formatting:

2. Implement Pre-commit Hooks

Use Husky and lint-staged to enforce quality gates:

3. Use Descriptive Variable and Function Names

Choose clear, self-documenting names:

Accessibility

1. Use Semantic HTML

Always use appropriate semantic HTML elements:

2. Provide ARIA Labels

Add ARIA labels for screen readers:

3. Manage Focus

Ensure keyboard navigation works correctly:

4. Test with Screen Readers

Manually test components with screen readers (NVDA, JAWS, VoiceOver) to ensure they’re usable.

Security

1. Respect Field-Level Security

Use Lightning Data Service which automatically enforces FLS:

2. Sanitize User Input

When displaying user input, use Lightning base components that handle sanitization:

3. Use @AuraEnabled(Cacheable=true) Carefully

Only cache Apex methods that return data accessible to all users:

Documentation

1. Add JSDoc Comments

Document public APIs and complex logic:

2. Include Component-Level Documentation

Add XML metadata describing the component:

3. Create Code Tours

For complex features, create CodeTour walkthroughs to help developers understand the implementation.

Summary

Following these best practices will help you build Lightning Web Components that are:
  • Maintainable: Clear structure and consistent patterns
  • Performant: Optimized for speed and efficiency
  • Testable: Comprehensive test coverage
  • Accessible: Usable by everyone
  • Secure: Following Salesforce security best practices
  • Scalable: Ready to grow with your application