Closed gaearon closed 6 years ago
I really like direction where this is going. I would say that agree with you both @gaearon and @jxnblk, you both have valid points. Coming from an asp.net background (yes it's been a while but anyway) I should say that I've always liked the way asp.net webforms did its components with "layout slots" like GridView.Header etc and then handed over the actual rendering of the header slot to the user but still kept the basic layout of GridView to be handled by GridView. I've tried to do these things in react and I've always came up with solutions that uses styled-components (or what not) to handle positioning via plain css or something. Or the prop way that react generally offers (which of course works but...again...well) I do hope that macro components seem to at least "solve" some of the "confusion" around this. Thanks!
Hello! What about key property for doing such things? It is very easy to map children with key, and it seems that it is right way for mapping seeblings.
I do agree that key is preferable to a custom "name" prop here. But I think explicit "holes" are even better.
Published v2 with an API largely based on this proposal. Closing this out, but feel free to continue the discussion here or in other issues
After a discussion in https://github.com/jxnblk/macro-components/issues/3 I think I have a better idea of what you're aiming at. Here's a proposal of how we could change the API. I think it allows us to:
displayName
.name
will get minified (and thus broken)name
property and don't overload it with custom meaningThe main idea of the proposal is that since the "macro" component knows which children it allows, it should just put them on itself as fields. Then we don't have to "guess" which component corresponds to which "slot" by its name because the mapping is explicit.
Rewritten README examples with the proposed API:
Note how there are two changes:
macro
first argument is now a mapping from the "slot name" to a component. With ES6 shorthand syntax it's super concise.This solution is versatile. Here is the second example:
You can always be even more explicit too if you prefer this style:
But what if we want to have multiple elements of the same type? No
name
prop, we just customize the mapping.There is no need for a special API to use the component type. It is already enforced automatically.
I hope this makes sense!