I need help in understanding the below issue. Looks like a bit weird one or might be i am not aware of basics of LWC. 3 ways to test and understand this :-
- Comment connectedCallback Code, In this case by default getter method is called ,its value is getting displayed on HTML but its value is not changing on button click its remains the same. Why?
- Uncomment connectedCallback Code, in this case ,connectedcallback value is not getting dispalyed. Seems like getter method is overriding everything as value inside getter methd is only getting displayed.
- On commenting getter method ,connectedCallback() value is getting displayed and on button click its value is getting changed,which seems OK to me. So ,my doubt is when to use ConnectedCallback and when to use getter method.
JS controller :-
export default class ParentWebComp extends LightningElement {
strVar='';
get message(){
return this.strVar='LWC getter';
}
connectedCallback(){
this.strVar='LWC connectedCallback';
}
handleClick(event){
this.strVar='Due to button Click';
}
HTML template:-
{message}<br/>
{strVar}<br/>
<lightning-button onclick={handleClick}/>