-
I'm in the middle of upgrading a code base currently using lit 2 to 3 and I'm confused about the behaviour of the new automatic calls to import { html, css, LitElement, PropertyDeclarations } from 'lit';
export class Base extends LitElement {
static properties: PropertyDeclarations = {
numClicks: { type: Number, state: true },
};
protected _numClicks = 0;
set numClicks(value: number) {
this._numClicks = value;
}
get numClicks() {
return this._numClicks;
}
render() {
return html`
<p>You have clicked ${this.numClicks} times</p>
<button @click="${this.onClickButton}">Click</button>
`;
}
onClickButton() {
this.numClicks++;
}
}
export class Child extends Base {
// static properties: PropertyDeclarations = {
// numClicks: { type: Number, state: true },
// };
set numClicks(value: number) {
// const oldValue = this._numClicks;
this._numClicks = value * 2;
// this.requestUpdate('numClicks', value);
}
get numClicks() {
return this._numClicks;
}
} If I redeclare the property in the child class it is called automatically again, but redeclaring properties is not something we had to do previously. It also works if I call Playground: https://stackblitz.com/edit/typescript-sme8a5?file=index.ts |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
The call to lit/packages/reactive-element/src/reactive-element.ts Lines 750 to 761 in c7134a4 |
Beta Was this translation helpful? Give feedback.
-
Yeah, I can see how that's a bit confusing. Defining Subclases are on their own, so you have 2 options if you don't want to use
|
Beta Was this translation helpful? Give feedback.
The call to
requestUpdate
is injected into the setter (the setter is redefined that calls the original setter as you wrote it, and then callrequestUpdate
.So if you don't call
super.numClicks = value
, you won't triggerrequestUpdate
.So you're seeing the expected behavior.
lit/packages/reactive-element/src/reactive-element.ts
Lines 750 to 761 in c7134a4