In LWC properties are similar to JavaScript properties. In js file of component you can declare your properties using camel casing and in your Html you can write same name using kebab casing called attribute this way both will be mapped to each other. Ex : In js file I have a property as showFeature and its corresponding attribute in Html file written as show-feature . This way both will be mapped to each other and value of property will be visible in html.
There are two types of properties in LWC
- Reactive Properties
- Non-reactive Properties
Before starting with this lets understand with decorators
What is decorator?
Decorator is a part of ECMA script and used to add extra functionality in your function or methods. In LWC we use 3 decorator to enhance the functionality of our property or function.
@track
@api
@wire
Reactive Properties
If value of any reactive property changes and that is used in any template then that template will re-render and it will evaluate again all the expression written in template. It can be of two type.
- Public properties
- Private properties
Also Read: Difference: Data Loader vs Import Wizard
Public Properties
In order to expose any property as public we decorate it as @api. Along with decoration you have to import this in your js file as
import { LightningElement, api } from 'lwc';
These public properties can be used by the parent component. The component that set a property as public can set only its default value. It’s not like you can’t change but it’s preferable that parent change and play with its value.
Private Properties
In order to expose any property as public we decorate it as @api. Along with decoration you have to import this in your js file as
import { LightningElement, track } from 'lwc';
These can be used by the component in which it is defined.