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

JSX Spec has been updated (Fragment syntax) #19094

Closed
Jessidhia opened this issue Oct 11, 2017 · 8 comments
Closed

JSX Spec has been updated (Fragment syntax) #19094

Jessidhia opened this issue Oct 11, 2017 · 8 comments
Assignees
Labels
Committed The team has roadmapped this issue Domain: JSX/TSX Relates to the JSX parser and emitter Suggestion An idea for TypeScript

Comments

@Jessidhia
Copy link

The JSX spec has been updated to support Fragments, which are a shorthand syntax for using a specific opaque class from the React package. Spec PR is at facebook/jsx#93.

Fragments are written by completely empty tags (only whitespace is allowed between < and >), and look like <>{'child1'}{'child2'}{'child3'}</>. This is not written down in the PR's text itself as it is only an AST update, but according to facebook/jsx#93 (comment) it should desugar to <React.Fragment>{'child1'}{'child2'}{'child3'}</React.Fragment>.

The intent is to make returning multiple elements from a component more ergonomic -- instead of returning an array where each child needs to be keyed, return a fragment that preserves the optional key reconciling behavior.

@DanielRosenwasser
Copy link
Member

Thanks for actually providing an explanation @Kovensky! I'll note that the author also elaborated here as well.

@DanielRosenwasser DanielRosenwasser added Domain: JSX/TSX Relates to the JSX parser and emitter Suggestion An idea for TypeScript labels Oct 11, 2017
@clemmy
Copy link

clemmy commented Oct 11, 2017

Just wanted to point out that since comments are treated as whitespace in Javascript, they're also allowed between < and >. For example, the following is valid:

< /*
 I'm a multiline comment
*/
>
</>

@RyanCavanaugh
Copy link
Member

Sounds good. A few questions:

  • What's the emit under --jsx react ? React.createElement(React.Fragment, ...) ?
  • Under --jsx preserve, should we exactly preserve <>, or emit <React.Fragment> ?
  • Confirming that <> is not allowed to have any attributes

@RyanCavanaugh RyanCavanaugh added the Committed The team has roadmapped this issue label Oct 11, 2017
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 2.7 milestone Oct 11, 2017
@clemmy
Copy link

clemmy commented Oct 11, 2017

@RyanCavanaugh I think I can answer a few of those:

What's the emit under --jsx react ? React.createElement(React.Fragment, ...) ?

Yup.

Confirming that <> is not allowed to have any attributes

Yup.

@Jessidhia
Copy link
Author

Jessidhia commented Oct 12, 2017

Preserve <> would be best; but would be incompatible with tooling that has not yet been updated to support it. Emitting <React.Fragment> would be backwards-compatible but is not really preserving JSX, is it? :)

Maybe --jsx preserve-jsx1 (or just --jsx jsx1) for converting to <React.Fragment>, with --jsx preserve preserving the input as <>.

EDIT: Preserving <> is important in case the user is using JSX with another library that has its own native fragment that is not React.Fragment.

@RyanCavanaugh
Copy link
Member

I think the EDIT above is the key point. Let's stick with <> for preserve emit.

@jtmthf
Copy link
Contributor

jtmthf commented Oct 12, 2017

Would a --jsxFragmentFactory option be needed as well for keeping compatibility with alternative JSX libraries?

@uniqueiniquity
Copy link
Contributor

Referencing facebook/jsx#94 here so I don't forget about it. :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Committed The team has roadmapped this issue Domain: JSX/TSX Relates to the JSX parser and emitter Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants