-
-
Notifications
You must be signed in to change notification settings - Fork 926
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow children to be specified as a single function returning a vnode tree #2050
Comments
Yes, if you follow React at all, this sounds a lot like their new "render props" fad. But it's not something Mithril users haven't been doing for a while through attributes instead. I just wanted to make a common pattern easier. |
@isiahmeadows This would be a -0.1 from me, in that it doesn't add any functionality. This seems as clear as your proposal: var List = {
view({attrs: {items, render = items => items + ""}}) {
return m("div", render(transform(items)))
}
}
m(List, {
items: [...]
render: items => items.map(item => m("div", ...))
}) |
Okay. That's basically what I did with my |
this is a somewhat contrived example, but is there a recommended decorator pattern to augment a component that has children? the use case is to let a user defined component be left intact, but let's say we want to provide a way decorate a component with functionality via a 3rd party util. |
@cavemansspa There's a |
Re-opening this following #2106. cc/ @barneycarroll. |
I missed this when it was first raised and wished I hadn't. Since then I've put a lot of work in exploring 'function-children' components. I last left #2106 with the thought that this was an acceptable contrivance for authors of a very specific kind of component. But the more I think about it, the more I get @pygy's uneasy change of mood, and I'm more inclined to frame this as a case of the current vnode normalisation implementation being dangerously eager (as in WTF) on principle rather than the proposed use case necessarily deserving first class treatment (why shouldn't it?). I've been racking my brain over a few avenues for resolving this ambiguity, and I think a couple actions should be taken:
I think that would also go some way to addressing Isiah's previously stated gripes with the contradictions of the documentation's advice on how to treat children. All in all we might all have been a bit guilty of condensingly paternalistism. The kids are alright. |
I need to think 2) through, but at a glance, it looks like a good plan (for v2). How is 1) related to this? |
Sorry @pygy I missed this response. I am about to file an issue regarding the specifics of point 1, which has since been revealed to be a bug in its own right. It relates to this issue inasmuch as Mithril's vnode type is very tolerant of what input can constitute a vnode in part because it leaves the door open to complex types with |
In light of some recent discussion on #2275, I'm resurrecting this. |
@isiahmeadows I think current v2 behaviour is good enough, personally. Check it out: we can event do the neat-o React array destructuring thing! const ViewComponent = {
view: ({children: [view]}) =>
view('Hello!'),
} |
Yeah, since that works, I'm closing it for now. I've got ideas in #2279/#2295 (comment), but I'm closing this in favor of what turns of that. |
Expected Behavior
This came up on Gitter in a discussion there.
Basically, I'd like to see this to happen:
.children
altogether.Current Behavior
Currently, functions are just
toString()
'd, which is hardly helpful ever.Possible Solution
A few small changes to
Vnode.normalize
,Vnode.normalizeChildren
, andm
.Steps to Reproduce (for bugs)
Context
This is to support reuse of child vnodes without actually requiring identity (which we all know is unsafe). It also allows children to be parameterized in cases like with React's
react-motion
, and it'd make Mithril a little more useful for cases where you have to deal with animations/etc. (and other changing data independent of the global model state).A couple other examples where this could've improved or otherwise affected API design:
mithril-infinite
could've opted to use children as a function instead ofbefore
/after
.mithril-helpers/self-sufficient
could've opted to use children as a function instead of relying brittily on inheritance and unsafely overriding hooks. Edit: It currently does similar now, but does so in a way that's slightly awkward.Also, if you've ever used a pattern like this, this feature requests aims to reify and partially replace it:
Your Environment
The text was updated successfully, but these errors were encountered: