diff --git a/js/src/common/Component.ts b/js/src/common/Component.ts index 47f28aef4..91a0524fa 100644 --- a/js/src/common/Component.ts +++ b/js/src/common/Component.ts @@ -24,9 +24,7 @@ export default abstract class Component implemen abstract view(vnode: Mithril.Vnode): Mithril.Children; oninit(vnode: Mithril.Vnode) { - this.initAttrs(vnode.attrs); - - this.attrs = vnode.attrs; + this.setAttrs(vnode.attrs); } oncreate(vnode: Mithril.VnodeDOM) { @@ -34,9 +32,7 @@ export default abstract class Component implemen } onbeforeupdate(vnode: Mithril.VnodeDOM) { - this.initAttrs(vnode.attrs); - - this.attrs = vnode.attrs; + this.setAttrs(vnode.attrs); } /** @@ -67,6 +63,18 @@ export default abstract class Component implemen return m(this as any, componentProps, children); } + private setAttrs(attrs: T) { + this.initAttrs(attrs); + + if (attrs && 'children' in attrs) { + throw new Error( + 'The "children" attribute of attrs should never be used. Either pass children in as the vnode children or rename the attribute' + ); + } + + this.attrs = attrs; + } + protected initAttrs(attrs: T): T { return attrs; }