Build the @for block's list reconciliation: track keys, moves, swaps, in-place updates and duplicate keys

hardAbout 60 minutesUpdated CodingProangularcontrol-flowreconciliationdiffingtrack-byalgorithms
Not solved

Share this question

Problem

Angular 17's @for (item of items; track item.id) block re-renders a list by reusing a view (a piece of DOM plus its context) for every item whose track key survives, moving it if needed, and creating or destroying only what changed. The algorithm behind it is a common "how does Angular do it" question, and the track expression is a common performance question. Build the reconciliation for one @for block.

const repeater = createRepeater({ track: (i, user) => user.id });
repeater.update([asha, ravi, meera]);    // { created: 3, destroyed: 0, moved: 0, updated: 0 }
repeater.update([meera, asha, ravi]);    // { created: 0, destroyed: 0, moved: 1, updated: 0 }
repeater.update([{ ...meera, online: true }, asha]); // updated: 1, destroyed: 1

Ref. UR-1454 · © UIRound · uiround.com/questions/build-the-for-blocks-list-reconciliation-track-keys-moves-swaps-in-place-updates-and-duplicate-keys

Stuck? Take a hint

Try it in the editor

Run your code as often as you like. Submitting it to the full test suite is part of Pro.

Private, and saved in this browser as you type. All your notes are on My lists.

Every question and answer7 days, one payment