Sub topic 7
Getting data into a component
This chapter assumes you can build a basic Lightning web component and write an @AuraEnabled Apex method. It also assumes you have already exposed a component to Experience Builder. Here you will load Case records into a site component in two ways: with @wire and with an imperative call.
Two ways to load data
A component can get data from Apex in two ways.
- Wire: you declare what you need, and the platform calls Apex and re-calls it when inputs change.
- Imperative: you call the Apex method yourself, at a time you choose, and handle the promise.
| Row | Wire | Imperative |
|---|---|---|
| Triggered by | Component load and reactive parameter changes | Your code, such as a click handler |
| Apex requirement | `cacheable=true` | Optional |
| Allows DML (create, update, delete) | No | Yes, with non-cacheable methods |
| Result shape | `{ data, error }` | Resolved value or thrown error |
Reading data with @wire
The CaseList component from the earlier chapters wires a cacheable Apex method:
import { LightningElement, api, wire } from 'lwc';
import isGuest from '@salesforce/user/isGuest';
import getMyCases from '@salesforce/apex/CaseListController.getMyCases';
export default class CaseList extends LightningElement {
@api maxRows = 5;
isGuest = isGuest;
@wire(getMyCases, { maxRows: '$maxRows' })
cases;
get hasCases() {
return Boolean(this.cases && this.cases.data && this.cases.data.length);
}
}The $ prefix makes the parameter reactive. When an administrator changes maxRows in the page editor, the wire runs again. Without the $, the string 'maxRows' is sent as a literal value, and the call fails.
The wire first provisions cases with data and error both undefined. That is why hasCases checks each level before reading length. The template can then show the "No cases found" message while loading and after an empty result.
Reading data imperatively
Use an imperative call when the user triggers the load, or when the method changes data:
import { LightningElement, api } from 'lwc';
import getMyCases from '@salesforce/apex/CaseListController.getMyCases';
export default class CaseListManual extends LightningElement {
@api maxRows = 5;
cases = [];
error;
async handleRefresh() {
try {
this.cases = await getMyCases({ maxRows: this.maxRows });
this.error = undefined;
} catch (e) {
this.error = e;
this.cases = [];
}
}
}The imported method returns a promise. Parameters go in a single object whose keys match the Apex parameter names.
Common errors
- Missing
cacheable=true. Wiring a non-cacheable method fails. If the method changes data, call it imperatively instead. - Stale data after an update. Wired results are cached. After a change, call
refreshApexon the wired value, or reload imperatively. - Ignoring
error. A guest or external user without class access gets an error and no data. Log or display it while you test. - Mutating
data. Wired results are read-only. Copy the data first, for example with[...this.cases.data].
Exercise
A wired Apex method does not rerun when the administrator edits maxRows in Experience Builder. The wire is declared as @wire(getMyCases, { maxRows: 'maxRows' }). What is wrong, and how do you fix it?
Show answer
The parameter is not reactive. Without the $ prefix, the literal string "maxRows" is passed instead of the property value. Change it to { maxRows: '$maxRows' } so the wire reruns when the property changes.