Skip to content

Commit cf350a7

Browse files
authored
Fix #17
1 parent ffbedef commit cf350a7

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

docs/keys.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Documentation on the special "key" attribute in Mithril.js, which tracks vnodes'
1616
- [Mixing key types](#mixing-key-types)
1717
- [Hiding keyed elements with holes](#hiding-keyed-elements-with-holes)
1818
- [Duplicate keys](#duplicate-keys)
19+
- [Using objects for keys](#using-objects-for-keys)
1920

2021
---
2122

@@ -551,3 +552,16 @@ var things = [
551552
```
552553

553554
Mithril.js uses an empty object to map keys to indices to know how to properly patch keyed fragments. When you have a duplicate key, it's no longer clear where that element moved to, and so Mithril.js will break in that circumstance and do unexpected things on update, especially if the list changed. Distinct keys are required for Mithril.js to properly connect old to new nodes, so you must choose something locally unique to use as a key.
555+
556+
#### Using objects for keys
557+
558+
Keys for fragment items are treated as property keys. Stuff like this will not work like you think it will.
559+
560+
```javascript
561+
// AVOID
562+
things.map(function(thing) {
563+
return m(Thing, {key: thing, thing: thing})
564+
})
565+
```
566+
567+
If you have a `toString` method on it, that would get called, and you'd be at the mercy of whatever it returns, possibly not realizing that method is even being called. If you don't, all your objects will stringify to `"[object Object]"` and thus you'll have a nasty [duplicate key](#duplicate-keys) problem.

0 commit comments

Comments
 (0)