Skip to content

Simplify component constructor to function that returns either a view function or a vnode tree #2690

@dead-claudia

Description

@dead-claudia

Mithril version:

Platform and OS:

Project:

Is this something you're interested in implementing yourself?

Description

Depends on #2688 and #2689 - you can see the first because of the two-argument view. It's easier to explain relative to this.

Replace these idioms:

const Comp = {
    view(vnode, old) {
        return tree
    }
}

function Comp(initial) {
    return {
        view(vnode, old) {
            return tree
        }
    }
}

With these:

function Comp(attrs, old) {
    return tree
}

function Comp(initial) {
    return (attrs, old) => {
        return tree
    }
}

As the rest of vnode.* properties are subsumed with other vnodes, I'd just provide the attrs themselves instead of full vnodes. (Vnode children would be moved to attrs.children to align with virtually every other framework out there - I don't see the benefit in us remaining special here.)

Why

  1. It's simpler to write.
  2. It's simpler to implement.
  3. It's lower overhead to invoke.
  4. Not much of a reason, but TS compatibility would be increased with this. If TS isn't, it'd be a much easier ask to get them to correct the incompatibility.

Possible Implementation

  1. In createNode, change state initialization to just this:
var instance = (0, vnode.tag)(vnode.attrs)
if (typeof instance === "function") {
    vnode.state = instance
    vnode.instance = instance(vnode.attrs)
} else {
    vnode.state = vnode.tag
    vnode.instance = instance
}
// render vnode.instance as usual
  1. In updateNode, invoke (0, vnode.state)(vnode.attrs, old.attrs) instead of vnode.state.view(vnode, old).

Open Questions

Metadata

Metadata

Assignees

Labels

Area: CoreFor anything dealing with Mithril core itselfType: Breaking ChangeFor any feature request or suggestion that could reasonably break existing codeType: EnhancementFor any feature request or suggestion that isn't a bug fix

Type

No type

Projects

Status

Completed/Declined

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions