mediumcodingjavascript~12 min
Reconcile a Keyed List
Objective: See why stable keys preserve identity and index keys do not.
Implement `reconcile(prev, next)`, comparing two lists of `{ key }` items the way React decides what to keep, mount and unmount.
- Return `{ kept, added, removed }` as arrays of keys.
- `kept` contains keys present in both lists, in the order of `next`.
- `added` contains keys only in `next`; `removed` only in `prev`, each in their list's order.
- Throw an Error if `next` contains a duplicate key.
JavaScript
⌘/Ctrl + Enter to run tests · Esc then Tab leaves the editor
Output
> Nothing has run yet.
Interview follow-up questions
- What goes wrong when you use the array index as a key and items can be reordered?
- How does changing a component's key reset its state?
- Why does React warn about duplicate keys?