4

While understanding code snippets provided by Salesforce. There are a few things which were confusing. Like what is @api, @wire.

I googled for them, came to know they are called decorators. Responsible for 'import' things. Means wiring up the functionality with current logic.

I am actually finding what is the use of @api, @wire ...etc. Is it defined somewhere that where to use which decorators?

1 Answer 1

7

Decorators are nothing but annotations just like the one you use in your apex class like @auraenabled which adds functionality to the regular apex method to be able to be called from a lightning component, but decorators here are annotations in the context of javascript allowing them to provide functionality for properties or functions!

There are 3 decorators specific to lightning web components:

@api : To expose a public property(Think of properties as your aura attributes), decorate it with @api. Public properties define the API for a component. An owner component that uses the component in its markup can access the component’s public properties. Public properties are reactive. If the value of a reactive property changes, the component’s template rerenders any content that references the property.To expose a public method, decorate it with @api. Public methods are part of a component’s API. You can use a JavaScript method to communicate down the containment hierarchy. For example, an owner calls a method on a child component that it contains.

@track : To track a private property’s value and rerender a component when it changes, decorate the property with @track. Tracked properties are also called private reactive properties.

@wire : To read Salesforce data, Lightning web components use a reactive wire service. When the wire service provisions data, the component rerenders. Components use @wire in their JavaScript class to specify a wire adaptor or an Apex method.

There is lot more documentation you can read about decorators and how to use them in the developer guide available in the component library.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .