-
Notifications
You must be signed in to change notification settings - Fork 132
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
JSX Spread tag proposal #63
Comments
I would say that spreading is not what you want, you want the same behavior as you would get with arrays (if you ignore the key warning) with it being its own hierarchy and implicit indices computed accordingly. A previously discussed syntax is Also, I imagine if we want to support spreading there's already a solution that wouldn't necessarily introduce new syntax |
Don't I have to key the fragments? Actually come to think of it, Is there any conceptual difference in React between
? |
@quassnoi Using
Yeah, I'm not 100% sure TBH. It may just have been "informal/internal" discussions in other places.
Yes, a very important one. Removing |
Are you saying I can just pass an array to |
@quassnoi Honestly not sure, but you could easily have a wrapper that does it, it's trivial logic. |
This is a feature request.
It is a common request from developers to allow returning multiple elements from inline expressions in JSX.
http://stackoverflow.com/questions/23840997/how-to-return-multiple-lines-jsx-in-another-return-statement-in-react
A use case for this would be this pseudocode:
which would have React generate either
<Parent><Child/></Parent>
or<Parent><Sibling1/><Sibling2/><Sibling3/></Parent>
, depending on the value of the flag.As it stands now, this would not work, as two or more consecutive tags would not transpile to a single expression.
There are several approaches to that problem:
Wrap the elements into some kind of a parent tag:
This is not always acceptable.
Use arrays:
This would make React complain on the elements in the second array having no unique
key
property, and adding that property would take some extra effort.Use keyed fragments (Add fragment API to allow returning multiple components from render react#2127)
This requires assigning arbitrary keys to the fragments, the syntax is quite cumbersome and it's not JSX.
Use
createElement
directly, making benefit of ES6 spread syntax:This is the most straightforward way, as it would be equivalent to either
React.createElement('parent', {}, <Child/>)
orReact.createElement('parent', {}, <Sibling1/>, <Sibling2/>, <Sibling3/>)
, but it's not JSX either.My proposal is to make use of the latter solution in JSX, extending JSX with a Spread tag:
<...>
This tag could only be used as a non-top element in a JSX tree and could only contain a single JS expression in curly braces, which must evaluate to an array.
This Spread tag
<...>{ expression }</...>
would transpile to ES6 array spread syntax:...(expression)
, so that<Parent><a/><...>{ expression }</...><b/></Parent>
becomesReact.createElement('parent', {}, React.createElement('a', {}), ...(expression), React.createElement('b', '{}'))
This way, the original problem could be solved by writing:
which would transpile as follows:
This is almost as simple as the naive solution at the very top, and it produces the exact effect most developers are after: variable number of arguments to
createElement
with no need to create keys or wrap elements into dummy tags.Also, it's a pure JSX feature which does not introduce any new React code.
The text was updated successfully, but these errors were encountered: