Skip to content
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 components to render abstract elements(feature request) #2449

Closed
tiye opened this issue Nov 2, 2014 · 10 comments
Closed

allow components to render abstract elements(feature request) #2449

tiye opened this issue Nov 2, 2014 · 10 comments

Comments

@tiye
Copy link

tiye commented Nov 2, 2014

Every component we create we have to return an actiaul element. But in some cases we just don't actually need that. For example, when we attempt to transitions like what Famo.us does, we would calculate every position by hand, and such a wrapper becomes unnesseccery. So how about introducing something like "abstract elements" to do that?

@syranide
Copy link
Contributor

syranide commented Nov 2, 2014

This is simply a limitation of React's current "DOM implementation" and will certainly be dropped in the future. You can return null at current but it will simply translate to <noscript />.

However, I'm not entirely sure what you're asking for, if you intend to render "dummy components" which never have any DOM themselves then while it will probably be supported in the future, it will probably not be considered good practice.

@tiye
Copy link
Author

tiye commented Nov 3, 2014

@syranide I should post some example. Suppose such a component:

React.createClass
  render: ->
    React.createAbstractElement 'group-a', data: {},
      React.createElement 'div', data: {}, 'something'
      React.createElement 'div', data: {}, 'something too'

It renders:

<div></div>
<div></div>

But shows this in React devtool

<group-a>
  <div></div>
  <div></div>
<group-b>

So <group-a> is not actually an element. This could be useful in some cases where people don't want it, such as the components mounted to <body> by React.render(component, body). Also in those cases where each child of <group-a> is positioned absolute or fixed it would allow DOM to be simpler.

@syranide
Copy link
Contributor

syranide commented Nov 3, 2014

@jiyinyiyong Ah now I see, same answer though, it's a limitation of the current "DOM implementation" in React. There's nothing wrong with your example, but group-a must wrap this.props.children in a DOM component for it to work or it will return an array (which is not allowed). AFAIK devs have expressed intent to fix this in the future.

@wenbing
Copy link

wenbing commented Nov 4, 2014

glad to here that
On 2014年11月3日 周一 at 18:54 Andreas Svensson [email protected] wrote:

@jiyinyiyong https://github.com/jiyinyiyong Ah now I see, same answer
though, it's a limitation of the current "DOM implementation" in React.
There's nothing wrong with your example, but group-a must wrap
this.props.children in a DOM component for it to work or it will return
an array (which is not allowed). AFAIK devs have expressed intent to fix
this in the future.


Reply to this email directly or view it on GitHub
#2449 (comment).

@leoasis
Copy link

leoasis commented Nov 5, 2014

@syranide Do you know what is the actual limitation for allowing components to return arrays as a render result? I'm curious to know and investigate deeper.

@syranide
Copy link
Contributor

syranide commented Nov 5, 2014

@leoasis I haven't really investigated the exact obstacles, but broadly speaking, the problem is the current implementation expects all components to correspond to exactly one DOM node (can be shared) which is a very simple case to handle (in-terms of lazily associating node and component, and also mutating the root components). To be able to handle 0 or 2+ is a lot more complex and none of that "infrastructure" currently exists in React (components would need to be aware of sibling components, etc, etc).

@leoasis
Copy link

leoasis commented Nov 5, 2014

@syranide I see, yeah, that seems to be something hard to tackle, since other libraries have the same limitation, so it is definitely not a trivial problem to solve. Will check the source for this and try to understand a bit better how that assumption of one component to one DOM element makes the code easier. Thanks!

@syranide
Copy link
Contributor

syranide commented Nov 5, 2014

@leoasis If you're looking to contribute in general I would recommend looking to other areas of React. I would expect this to affect the internal API considerably in quite non-trivial ways and even if you managed to get it working, that it may just be too much friction to realistically get it approved. That being said, figuring out what needs to be done and pushing bite-sized PRs is always a possibility, or do you have any objections @sebmarkbage ?

@leoasis
Copy link

leoasis commented Nov 5, 2014

@syranide I'll do that as an exercise to get to know the internals of React. Honestly, especially given that you guys are awesome and couldn't figure it out yet, I don't think I will come out with something to change. It was only curiosity. Anyway, if there is already some research on this, and the pain points have already been identified, I'd be more than helpful to read through that and try to contribute!

@sophiebits
Copy link
Collaborator

Closing this as a dupe of #2127.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants