PD1 Lightning Components 5 — Questions and Answers
Question 1: Which metadata file controls where a Lightning Web Component can be used (e.g., App Page, Record Page)?
- componentName.js-meta.xml (Correct answer)
- componentName.json
- package.xml
- componentName.html
Correct answer: componentName.js-meta.xml
The .js-meta.xml configuration file defines the component's targets (page types), isExposed setting, and properties visible in Lightning App Builder.
Question 2: In LWC, which decorator is used to consume data from a wire adapter or Apex method reactively?
- @api
- @track
- @wire (Correct answer)
- @reactive
Correct answer: @wire
@wire connects a component property or function to a wire adapter, automatically re-provisioning data when reactive properties change.
Question 3: What happens when you change a primitive value exposed via @api from the parent component in LWC?
- Nothing happens; @api properties are read-only from outside
- The component re-renders automatically because @api properties are reactive (Correct answer)
- You must call this.refresh() to trigger a re-render
- The change only takes effect after the next browser repaint
Correct answer: The component re-renders automatically because @api properties are reactive
Primitive @api properties are reactive by default in LWC, so changing them from the parent automatically triggers a re-render of the child component.
Question 4: In Aura components, what is the correct way to access a child component's method marked with `aura:method`?
- Use this.find('auraId').methodName() (Correct answer)
- Use document.getElementById('auraId').methodName()
- Use $A.getComponent('auraId').methodName()
- Use this.getEvent('methodName').fire()
Correct answer: Use this.find('auraId').methodName()
In Aura, this.find('auraId') retrieves a reference to a child component by its aura:id, and you can then call any aura:method defined on it.
Question 5: Which LWC template directive conditionally renders a block of HTML based on a boolean expression?
- <template if:true={condition}>
- <template lwc:if={condition}>
- Both A and B are valid in current LWC versions (Correct answer)
- <template show:if={condition}>
Correct answer: Both A and B are valid in current LWC versions
LWC supports both the older if:true/if:false directives and the newer lwc:if/lwc:elseif/lwc:else directives introduced in API version 55.
Question 6: What is a key difference between Aura application events and Aura component events?
- Application events bubble up the component hierarchy while component events are broadcast globally
- Application events are broadcast to all registered handlers in the app; component events propagate up the containment hierarchy (Correct answer)
- Component events can only be used in record pages; application events work everywhere
- There is no functional difference; they are interchangeable
Correct answer: Application events are broadcast to all registered handlers in the app; component events propagate up the containment hierarchy
Aura application events use a publish-subscribe model reaching any registered handler in the app, while component events propagate up through the component containment hierarchy.
Question 7: In LWC, which import path grants access to the currently logged-in user's ID?
- import userId from '@salesforce/user/Id'; (Correct answer)
- import { userId } from '@salesforce/user';
- import userId from '@salesforce/schema/User.Id';
- import userId from '@salesforce/currentUser/Id';
Correct answer: import userId from '@salesforce/user/Id';
The @salesforce/user/Id scoped module import provides the current user's record ID as a constant in LWC JavaScript files.
Which metadata file controls where a Lightning Web Component can be used (e.g., App Page, Record Page)?