Web Components

webc/lit-immutable-updates

A Lit reactive array is replaced, not mutated in place (push, splice, sort), unless requestUpdate() follows.

umiarkowanastatycznefe-web-components

Dlaczego

Lit decides whether to render by comparing the old and new value of a property. this.items.push(item) keeps the same array, so nothing changes from Lit's point of view: the new item does not appear until something else triggers a render, and child components that receive the array never update.

Poprawka

Assign a new array: this.items = [...this.items, item], this.items = this.items.filter(…), this.items = this.items.toSorted(compare). If a mutation is really needed for performance, call this.requestUpdate() right after it, knowing that children holding the same array still do not update.

Przykłady

// bad
add(item) { this.items.push(item) }
// good
add(item) { this.items = [...this.items, item] }

Źródła

Fakty

Identyfikator
webc/lit-immutable-updates
Waga
umiarkowana
Sprawdzanie
statyczne sprawdzane w kodzie źródłowym przez audit
Dotyczy plików
jsx
Tylko w projektach z
lit
Uczy jej skill
fe-web-components Web components and Lit
Od wersji
0.1.0

Plik reguły na GitHubie

Przeczytaj w terminalu

npx github:dawidolko/frontend-skills explain webc/lit-immutable-updates