PDI · Question #99
Which two scenarios require an Apex method to be called imperatively from a Lightning web component? Choose 2 answer
The correct answer is B. Calling a method that is not annotated with cacheable-true D. Calling a method that is external to the main controller for the Lightning web component. This question identifies specific technical conditions under which an Apex method must be called imperatively from a Lightning web component.
Question
Which two scenarios require an Apex method to be called imperatively from a Lightning web component? Choose 2 answer
Options
- ACalling a method that makes a web service callout
- BCalling a method that is not annotated with cacheable-true
- CCalling a method with the click of a button
- DCalling a method that is external to the main controller for the Lightning web component
How the community answered
(19 responses)- A11% (2)
- B74% (14)
- C16% (3)
Why each option
This question identifies specific technical conditions under which an Apex method must be called imperatively from a Lightning web component.
While a method making a web service callout must be called imperatively, the fundamental technical reason is that such methods cannot be `cacheable=true`; thus, option B provides the more general and direct underlying cause.
An Apex method must be invoked imperatively if it is not annotated with `@AuraEnabled(cacheable=true)`, because the `@wire` service requires methods to be cacheable for declarative data provisioning.
Calling a method with a button click is a common *trigger* for imperative invocation, but it is not the underlying technical *characteristic* of the Apex method itself that necessitates the imperative call. (Methods called this way are often not `cacheable=true`, fitting option B).
An Apex method needs imperative invocation when its execution is not automatically tied to the Lightning web component's initial rendering or reactive property changes, implying it is 'external' to the component's standard data-binding lifecycle and is invoked for specific actions or custom logic.
Concept tested: LWC imperative Apex method invocation
Source: https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.use_apex_wire_imperative
Topics
Community Discussion
No community discussion yet for this question.