Skip to content

Commit b48c3cf

Browse files
authored
Update components.md
1 parent ce449a7 commit b48c3cf

File tree

1 file changed

+74
-1
lines changed

1 file changed

+74
-1
lines changed

docs/components.md

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,80 @@ var MyModal = {
485485
}
486486
```
487487

488-
Instead, you should forward *single* attributes into vnodes:
488+
You could also run into errors if the element you're forwarding it to is in a fragment:
489+
490+
```javascript
491+
// AVOID
492+
var TaskItem = {
493+
// ...
494+
view: function(vnode) {
495+
return [
496+
m(".TaskList__TaskItem.Layout__Container", vnode.attrs, [
497+
// forwarding `vnode.attrs` here ^
498+
// ...
499+
]),
500+
m(".TaskList__TaskButtons.Layout__Container", [
501+
// ...
502+
]),
503+
]
504+
}
505+
}
506+
507+
var MyList = {
508+
view: function() {
509+
return m("div.TaskList__Container",
510+
Model.taskItems().map(function(item) {
511+
return m(TaskItem, {
512+
// This attribute gets forwarded to the
513+
// first element returned by `TaskItem`.
514+
// This makes the element keyed, and thus
515+
// causes the fragment it's in to have a
516+
// mix of keyed and unkeyed elements.
517+
//
518+
// Mithril checks for such mixed keys in
519+
// its vnode factories, and so you'd see
520+
// an error thrown.
521+
key: item.id,
522+
// ...
523+
})
524+
})
525+
)
526+
}
527+
}
528+
```
529+
530+
Instead, you should use [`m.censor`](censor.md) to remove all the problem lifecycle methods and attributes:
531+
532+
```javascript
533+
// PREFER
534+
var Modal = {
535+
// ...
536+
view: function(vnode) {
537+
return m(".modal[tabindex=-1][role=dialog]", m.censor(vnode.attrs), [
538+
// forwarding `vnode.attrs` here ^
539+
// ...
540+
])
541+
}
542+
}
543+
544+
// PREFER
545+
var TaskItem = {
546+
// ...
547+
view: function(vnode) {
548+
return [
549+
m(".TaskList__TaskItem.Layout__Container", m.censor(vnode.attrs), [
550+
// forwarding `vnode.attrs` here ^
551+
// ...
552+
]),
553+
m(".TaskList__TaskButtons.Layout__Container", [
554+
// ...
555+
]),
556+
]
557+
}
558+
}
559+
```
560+
561+
Also consider using a single attribute instead of forwarding them directly. It may make for clearer, cleaner code in some cases.
489562

490563
```javascript
491564
// PREFER

0 commit comments

Comments
 (0)