LWC Best Practices - MCP Knowledge

Based on Real Implementation Experience: These practices are aligned with official MCP guidance and production LWC architectures.

Overview

This guide summarizes best practices for:

Prerequisites

When to Use

Use This When

Core Concepts

Single Responsibility Components

Decorator Usage

Event Handling

Patterns and Examples

Pattern 1: Container-Presenter

Pattern 2: Service Modules

Edge Cases and Limitations

Q&A

Q: How many responsibilities should a single LWC have?

A: Ideally one primary responsibility. If a component handles data fetching, complex logic, and complex UI, consider splitting it into container and presenter components for maintainability.

Q: When should I use @wire vs imperative Apex?

A: Use @wire for reactive data needs (auto-refresh, record pages). Use imperative Apex for user-triggered actions (button clicks) or when you need more control over timing and error handling.