Wire Service & LWCs

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 automatically re-renders components when data changes. Thus, it eliminates the need for manual data fetch and re-rendering. […]

0

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:

  1. The wire service is reactive, meaning it automatically updates the component whenever underlying data changes.

  2. You can use the @wire decorator to connect a property or function in a component to an observable data stream.

  3. You can handle successful and unsuccessful data retrieval operations in your HTML template using the data and error properties.

  4. 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!

Cameron Ofoluwa
WRITTEN BY

Cameron Ofoluwa

22 Year Old Salesforce Developer @ Pogust Goodhead & Founder of SFDXHours.

Leave a Reply

Your email address will not be published. Required fields are marked *