mithril.js icon indicating copy to clipboard operation
mithril.js copied to clipboard

define component from function like React

Open Onesimu opened this issue 6 years ago • 7 comments

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

Onesimu avatar Aug 27 '19 01:08 Onesimu

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.

dead-claudia avatar Aug 27 '19 09:08 dead-claudia

@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).

spacejack avatar Aug 27 '19 13:08 spacejack

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).

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.

dead-claudia avatar Aug 27 '19 18:08 dead-claudia

@spacejack Can you give a simple demo, and can this pattern be used in JSX? Thank you.

Onesimu avatar Sep 04 '19 09:09 Onesimu

@Onesimu I think @spacejack had something like this in mind?

osban avatar Sep 04 '19 10:09 osban

I was thinking something simpler - I think the cached vnodes in your example are a bit confusing. Just a simple view function like this.

spacejack avatar Sep 04 '19 12:09 spacejack

Oh ok, yeah I put that in just to show it would be possible :)

osban avatar Sep 04 '19 12:09 osban