What is the Wire Service?
The wire service is a fundamental part of LWCs. It provides a streamlined way to read Salesforce data, metadata, and other values into a component. The wire service is reactive, which means it re-renders components when data changes. Therefore, it eliminates the need for manual data fetch and re-rendering.
Basic Wire Syntax
The wire service is accessed in a component’s JavaScript file via an imported wire
decorator from the lwc
module. The basic syntax is as follows:
import { LightningElement, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
export default class Example extends LightningElement {
@wire(getRecord, { recordId: '$recordId', fields: ['Account.Name', 'Account.Phone']})
account;
}
In this example, getRecord
is a wire adapter. Wire adapters are functions that connect a component to an ECMAScript 7 observable stream. The observable stream supplies data.
@wire Decorator
The @wire
decorator is used to wire a property or function in a component to a wire adapter’s stream of data. The decorator connects a specific data or metadata source to a property or function within your component.
Wiring to a Property
When wiring to a property, a JavaScript object is returned. This object contains two properties: data
and error
. You can use this information in your HTML template to handle both successful and unsuccessful data retrieval operations. For example:
import { LightningElement, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
export default class Example extends LightningElement {
@wire(getRecord, { recordId: '$recordId', fields: ['Account.Name', 'Account.Phone']})
account;
}
In this template
<template>
<lightning-card>
<template if:true={account.data}>
<div>Account Name: {account.data.fields.Name.value}</div>
<div>Account Phone: {account.data.fields.Phone.value}</div>
</template>
<template if:true={account.error}>
<div>Error: {account.error}</div>
</template>
</lightning-card>
</template>
Wiring to a Function
Wiring to a function gives you more control over how you handle the stream of data. The function you wire to receives an object with the same data
and error
properties. Here’s an example:
import { LightningElement, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
export default class Example extends LightningElement {
account;
error;
@wire(getRecord, { recordId: '$recordId', fields: ['Account.Name', 'Account.Phone']})
wiredAccount({ error, data }) {
if (data) {
this.account = data;
this.error = undefined;
} else if (error) {
this.error = error;
this.account = undefined;
}
}
}
Key Takeaways
Here are some crucial points to remember about the wire service:
-
The wire service is reactive, meaning it automatically updates the component whenever underlying data changes.
-
You can use the
@wire
decorator to connect a property or function in a component to an observable data stream. -
You can handle successful and unsuccessful data retrieval operations in your HTML template using the
data
anderror
properties. -
Wiring to a function gives you more control over data handling.
By understanding how to use the wire service in LWCs, you can create data-driven components that are reactive and efficient. Remember, practice is key to becoming proficient with these concepts. Start by creating simple components and then gradually move onto more complex ones. Good luck, and happy coding!