PDI · Question #8
A developer migrated functionality from JavaScript Remoting to a Lightning web component and wants to use the existing getOpportunities() method to provide data. What to do now?
The correct answer is A. The method must be decorated with (cacheable=true).. The developer needs to use an existing Apex method to provide data to a Lightning Web Component via a wired property, after migrating from JavaScript Remoting.
Question
A developer migrated functionality from JavaScript Remoting to a Lightning web component and wants to use the existing getOpportunities() method to provide data. What to do now?
Options
- AThe method must be decorated with (cacheable=true).
- BThe method must be decorated with @AuraEnabled.
- CThe method must return a JSON Object.
- DThe method must return a String of a serialized JSON Array.
How the community answered
(49 responses)- A92% (45)
- B4% (2)
- C2% (1)
- D2% (1)
Why each option
The developer needs to use an existing Apex method to provide data to a Lightning Web Component via a wired property, after migrating from JavaScript Remoting.
For an Apex method to be correctly used by a Lightning Web Component with a wired property for data retrieval, it must be annotated with `@AuraEnabled(cacheable=true)` to enable client-side caching, which improves component performance.
While `@AuraEnabled` is necessary, `(cacheable=true)` is also required specifically for methods invoked by wired properties in LWC to enable efficient client-side caching.
Apex methods invoked by LWCs typically return Apex data types (e.g., `List<Opportunity>`) that are automatically serialized to JSON, rather than explicitly returning a `JSON Object` string.
Apex methods invoked by LWCs typically return native Apex data types, and Salesforce handles the serialization to JSON automatically, so returning a serialized JSON Array string is not necessary or the standard practice.
Concept tested: Apex @AuraEnabled(cacheable=true) for LWC wire service
Source: https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.apex_wire_method
Topics
Community Discussion
No community discussion yet for this question.