Web Components

webc/lit-immutable-updates

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

moderatestaticfe-web-components

Why

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.

Fix

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.

Examples

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

Sources

Facts

Id
webc/lit-immutable-updates
Severity
moderate
Check
static checked in the source by audit
Applies to
jsx
Only in projects with
lit
Taught by the skill
fe-web-components Web components and Lit
Since version
0.1.0

Rule file on GitHub

Read it in the terminal

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