feat: Clarify keyed lists behavior
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 142
- Forks
- 230
- PR merge metrics
- No merged PRs in 30d
Description
[Regarding the page on lit-html Lists]
Coming from React and Stencil, I have been using this pattern in Lit:
myList.map(item=>
keyed(item.key, html`...`)
)
(which is similar to myList.map(item=><item key={item.key} ...> you would do in React and Stencil)
After reading Lit's source code, I realized that this is actually wrong and inefficient as it will not move the DOM nodes in response to myList items changing order, and instead re-create the DOM elements from scratch
- It might be just me, but explicitly calling out the above as wrong in the docs may be a good idea for transplants from other frameworks, and mentioning that
repeat()should be used instead- for new user this is a bit confusing - i.e why does
map()directive exists at all whenArray.prototype.mapdoes the job, and with less overhead? though a few other Lit directives likeifDefined()andwhen()are kind of needless API bloat too
- for new user this is a bit confusing - i.e why does
- Or better, would be nice if
lit-htmladds support for the above pattern and treats it equivalent to callingrepeat()(which, I can understand may be undesirable as it would require you to always bundlerepeat()even if not used).- As a workaround, for now I augmented my JSX->lit-html transformer to automatically convert code like
myList.map(item=><item key={item.key} ...>intorepeat(myList,item=>item.key,item=>html`<item ...>`
- As a workaround, for now I augmented my JSX->lit-html transformer to automatically convert code like
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked lit-html Lists documentation page and review its discussion of Array.prototype.map(), keyed(), and repeat(). Clarify the documented behavior and recommendation for reordered lists, including the relevant React and Stencil comparison. Done means the page makes the distinction and intended choice understandable to newcomers.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100