mithril.js
mithril.js copied to clipboard
define component from function like React
Mithril version: v2.0.4
Browser and OS: Chrome, Windows 10
Project: N/A
Is this something you're interested in implementing yourself? no
Description
A Mithril component is just an object with a view function. I think it can be more simple by just provide a function, then the framework can test when the argument type is function, it can simple to wrap it to a object. I think it don't need too much code, but convenient to simple component.
Why
simple component don't need to repeat the boilerplate code of {view: ...}, like React can do.
Possible Implementation
Open Questions
This has already been considered for v3 (and I really want to see it happen) as I'm looking to overhaul and simplify the component API, but this won't make it to v2.
@Onesimu With the current design, the way Mithril uses a function component allows you to store state in a closure (see this section of the docs.)
Also, you can currently use a plain function in your view with even less boilerplate. Instead of writing m(myViewFunction, attrs) you can simply write myViewFunction(attrs).
Also, you can currently use a plain function in your view with even less boilerplate. Instead of writing
m(myViewFunction, attrs)you can simply writemyViewFunction(attrs).
This is also a second reason why I've been hesitant. There exist patterns for the common case. I do want to point out this won't track identity, so it's not precisely equivalent.
@spacejack Can you give a simple demo, and can this pattern be used in JSX? Thank you.
@Onesimu I think @spacejack had something like this in mind?
I was thinking something simpler - I think the cached vnodes in your example are a bit confusing. Just a simple view function like this.
Oh ok, yeah I put that in just to show it would be possible :)