Development questions
What are the @api, @track and @wire decorators in LWC and when do you use each?
DevelopmentEasy
- @api makes a field or method public so a parent can set it (or call the method). Public properties are reactive but should be treated as read-only inside the child.
- @track makes a field deeply reactive, so mutations to properties of an object or elements of an array trigger a re-render. Since Spring '20 all fields are reactive for reassignment, so
@trackis only needed for in-place mutation of objects/arrays. - @wire connects a property or function to a wire adapter or Apex method, so data is provisioned reactively and re-fetched when reactive parameters (prefixed with
$) change.
javascript
import { LightningElement, api, wire } from 'lwc';
import getAccounts from '@salesforce/apex/AccountController.getAccounts';
export default class Demo extends LightningElement {
@api recordId;
@wire(getAccounts, { industry: '$industry' }) accounts;
}
lwcdecorators
More Development questions
- What is an Apex Trigger in Salesforce and when should you use it over Flows?
- What is the difference between before triggers and after triggers?
- Explain all Trigger context variables (isInsert, isUpdate, isDelete, isUndelete, isBefore, isAfter, isExecuting, size, operationType).
- What is the difference between Trigger.new and Trigger.old?
- In which trigger events are Trigger.new and Trigger.newMap available?
- In which trigger events are Trigger.old and Trigger.oldMap available?