Skip to content
StackDecode
AdvancedLesson 25 of 37

LWC: data and events

Read and write Salesforce data, and communicate between components.

javascript
import { LightningElement, wire, api } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import NAME from '@salesforce/schema/Account.Name';
import getTop from '@salesforce/apex/AccountController.getTop';

export default class AccountCard extends LightningElement {
  @api recordId;
  @wire(getRecord, { recordId: '$recordId', fields: [NAME] }) account;
  @wire(getTop) top; // Apex method marked @AuraEnabled(cacheable=true)

  get name() {
    return getFieldValue(this.account.data, NAME);
  }
}
  • Lightning Data Service (getRecord, lightning-record-form) handles caching and security without Apex.
  • Parent to child: @api properties. Child to parent: this.dispatchEvent(new CustomEvent('select', { detail: id })). Unrelated components: Lightning Message Service.