Concept and mechanism
Before creating Apex for an LWC, evaluate base components and Lightning Data Service for supported objects and operations. UI API enforces object, field, and record security. Wire provides a stream of values that should be treated as immutable and can emit again when dependencies change. A display transformation should use a local copy, preserving data ownership. Apex methods used with wire require cacheable true and mutation-free reading. A deliberate write, such as confirming a decision, needs an appropriate path, often an imperative call. This returns a Promise whose success or failure must be handled explicitly.
Guided application
A confirmed write does not automatically refresh every read path. For Apex wire, retain the provisioned result and pass it to refreshApex; the refresh Promise’s resolved value is not the data array to display. When imperative Apex changes records consumed by LDS, notify affected IDs through the appropriate mechanism. Do not assume a fixed TTL or repeat the business decision to force visual refresh. Between components, transmit a small contract: a CustomEvent with a primitive ID may suffice to communicate selection. If an object is needed, avoid handing out a mutable reference to the sender’s internal data. Distinguish pending, completed, and failed states in the operator experience.
A console displays an Apex-wired list and an LDS panel. After confirming a decision through Apex, it refreshes both consumers through their respective mechanisms. While rereading, it shows a pending state without saving again.
Common pitfalls
Mutating wired data; writing in a cacheable method; unhandled Promises; treating JavaScript Map as a guaranteed serializable parameter; one refresh assumed to cover every path.
Related topics: Data model, identity, and metadata · Collections and choosing configuration or code · Queries, DML, and error outcomes
Distinguish reactive reading, deliberate writing, and cache refresh. Communicate between components through small, clear contracts.
Reference: Client-Side Caching of Apex Method Results · Platform Developer guide Summer 25