Support for JSX Fragment Syntax
TypeScript 2.6.2 adds support for the new <>…</>
syntax for fragments in JSX.It is frequently desirable to return multiple children from a component.However, this is invalid, so the usual approach has been to wrap the text in an extra element, such as a <div>
or <span>
as shown below.
render() {
return (
<div>
Some text.
<h2>A heading</h2>
More text.
</div>
);
}
To address this pattern, React introduced the React.Fragment
component, which provides a dedicated way to wrap such elements without adding an element to the DOM.Correspondingly, the <>…</>
syntax was added to JSX to facilitate this new construct. Therefore, the above scenario becomes:
render() {
return (
<>
Some text.
<h2>A heading</h2>
More text.
</>
);
}
Under —jsx preserve
, the new syntax is left untouched for TypeScript emit. Otherwise, for —jsx react
, <>…</>
is compiled to React.createElement(React.Fragment, null, …)
, where React.createElement
respects —jsxFactory
.Note that it is an error to use <>…</>
when —jsx react
and —jsxFactory
are both enabled.
Please refer to the React blog for more details on fragments and the new syntax.