Web Components
webc/lit-class-fields
In JavaScript, a Lit reactive property is never also declared as a class field; the field hides Lit's accessor and updates stop.
poważnastatycznefe-web-components
Dlaczego
Lit makes each reactive property an accessor on the prototype. A class field with the same name (open = false) creates an own property on every instance that hides that accessor, so assigning this.open = true changes the value but never schedules a render: the component looks frozen and no error is shown.
Poprawka
In JavaScript, set initial values in the constructor after super(): this.open = false. With decorators in JavaScript, use accessor (@property() accessor open = false). In TypeScript, set useDefineForClassFields: false in tsconfig.json, or use declare or accessor on the field.
Przykłady
// bad
static properties = { open: { type: Boolean } }
open = false
// good
static properties = { open: { type: Boolean } }
constructor() {
super()
this.open = false
}Źródła
Fakty
- Identyfikator
webc/lit-class-fields- Waga
- poważna
- Sprawdzanie
- statyczne sprawdzane w kodzie źródłowym przez
audit - Dotyczy plików
jsx- Tylko w projektach z
lit- Uczy jej skill
fe-web-componentsWeb components and Lit- Od wersji
- 0.1.0
Przeczytaj w terminalu
npx github:dawidolko/frontend-skills explain webc/lit-class-fields