Wire Adapters & Functions in LWC

Let join us to learn about Wire Adapters & Functions in LWC. The wire service provisions an immutable stream of data to the component. Each value in the stream is a newer version of the previous value set. We call the wire service reactive in part because it supports reactive variables, which are prefixed with $. If a reactive variable changes, the wire service provisions new data.

Understand the wire service

Import a wire adapter using named import syntax. Decorate a property or function with @wire and specify the wire adapter. Each wire adapter defines a data type.

The wire service provisions an immutable stream of data. The data is not guaranteed to be the same, even when the adapterConfig object content is the same.

wire service in LWC

Example of Wire with uiRecordApi

Import References 

Using wire adapters you can import references to Salesforce Objects and Fields. Salesforce verifies that the objects and fields exist, prevents objects and fields from being deleted, and cascades any renamed objects and fields into your component’s source code. It also ensures that dependent objects and fields are included in change sets and packages. Importing references to objects and fields ensures that your code works, even when object and field names change.

Import a reference to an object

field Import reference

Import reference to relationship field

Tip: For objects, we use the naming convention OBJECTNAME_OBJECT. For fields, we use the naming convention FIELDNAME_FIELD. We use these naming conventions to make code easier to understand. They’re guidelines, not rules.

Import field and use it in a wire adapter config object

Import field and use it in a wire adapter config object

Import References for Compound Fields

@Wire with Property

Wiring a property is useful when you want to consume the data or error as-is. If the property decorated with @wire is used as an attribute in the template and its value changes, the wire service provisions the data and triggers the component to rerender. The property is private, but reactive

@Wire with Property

Decorate a function with @wire

Wiring a function is useful to perform logic whenever new data is provided or when an error occurs. The wire service provisions the function an object with error and data properties, just like a wired property.

Decorate a function with @wire

Wire Adapters & Functions in LWC Recording

YouTube video

Amit Chaudhary
Amit Chaudhary

Amit Chaudhary is Salesforce Application & System Architect and working on Salesforce Platform since 2010. He is Salesforce MVP since 2017 and have 17 Salesforce Certificates.

He is a active blogger and founder of Apex Hours.

Articles: 469

Leave a Reply

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