Sub topic 10
Wire service or imperative call: choosing
This chapter assumes you can write a basic LWC, know the @wire decorator exists, and have already called an Apex method imperatively from a button handler. Here you decide which approach fits a given requirement.
The working model
Both approaches call the same @AuraEnabled method. They differ in who decides when the call happens.
- Wire: The framework calls Apex for you. It runs when the component connects, and again whenever a reactive parameter (one prefixed with
$) changes. The result is delivered to a property or function that you declared. - Imperative: Your code calls the method. You choose the moment, such as a click, a timer, or the end of another call, and you handle the Promise yourself.
The method requirement is the first filter. A wired Apex method must be marked cacheable=true. An imperative call works with either setting.
Decision criteria
| Question | Wire | Imperative |
|---|---|---|
| Does the method perform DML? | Not allowed. Cacheable methods must be read-only. | Required. |
| Should data load automatically when the component appears? | Yes. | Only if you call it in `connectedCallback`. |
| Should the data follow a changing property? | Yes, through a reactive `$` parameter. | You must call again yourself. |
| Is the call triggered by a user action? | Awkward. | Natural. |
| Do you need to control timing, for example debouncing? | Limited. | Full control. |
| Do you need results in sequence, where call B uses the result of call A? | Hard. | Chain with `await`. |
| Do you want built-in caching? | Yes. | Only if the method is cacheable. |
The same read, written both ways
A wire version of the contact search from earlier chapters would look like this. The search term is reactive, so every change triggers a request.
import { LightningElement, wire } from 'lwc';
import searchContacts from '@salesforce/apex/ContactController.searchContacts';
export default class ContactSearchWired extends LightningElement {
searchTerm = '';
@wire(searchContacts, { searchTerm: '$searchTerm' })
contacts;
handleTermChange(event) {
this.searchTerm = event.target.value;
}
}This is short, but it sends a request on every keystroke. The imperative version in this tutorial waits for the Search button, so the user decides when to spend a call.
Wired results arrive as { data, error }. In the template you read contacts.data and contacts.error. You do not write loading code, but you also cannot easily show a spinner for a single refresh.
Common errors
- Wiring a method without
cacheable=true. The component fails to load or reports that the method is not cacheable. Fix it by marking read-only methods cacheable, or call imperatively. - Marking a DML method cacheable to satisfy the wire. Salesforce rejects DML inside cacheable methods at runtime. Keep writes in a separate non-cacheable method, as
updateEmaildoes. - Expecting a wire to re-run on a plain property. Only parameters written as
'$propertyName'are reactive, and the change must come through reassignment. - Mutating wired data. Wired results are read-only. Copy the data (
[...this.contacts.data]) before sorting or editing it. - Calling imperatively in
renderedCallbackwithout a guard. The callback runs after each render, so a call that sets state can loop. Use a flag, or move the call toconnectedCallback. - Stale cached results. A cacheable method may return cached data after a change elsewhere. Chapter-level refresh techniques cover this.
Mixing both
Mixed designs are common and fine. A component can wire a cacheable method to show a list on load, then call a non-cacheable method imperatively to save an edit. You can also call a cacheable method imperatively when you want caching but need to control timing.
Exercise
A component must show the current user's open cases when it loads, and must also let the user close a case with a button. Which Apex calls would you wire, and which would you call imperatively? Explain the cacheable setting for each method.
Show answer
Wire the method that lists open cases. It is read-only and should run on load, so mark it cacheable=true. Call the close-case method imperatively from the button's click handler. It performs DML, so it must not be cacheable. After closing a case, refresh the wired list so the closed case disappears.