Skip to content

Unexpected JSX output for children with jsx: react-jsx #40774

@nstepien

Description

@nstepien

TypeScript Version: 4.1.0-dev.20200925

Search Terms: jsx

Code

import type * as React from 'react';

console.log(
  <div>
    <div />
  </div>
)

console.log(
  <div>
    <div />
    <div />
  </div>
)

console.log(
  <div>
    {[1, 2].map(i => <div key={i}>{i}</div>)}
  </div>
)

Expected behavior:
Something closer to Babel's output, notice the lack of arrays for the children prop:

import { jsxs as _jsxs } from "react/jsx-runtime";
import { jsx as _jsx } from "react/jsx-runtime";
console.log( /*#__PURE__*/_jsx("div", {
  children: /*#__PURE__*/_jsx("div", {})
}));
console.log( /*#__PURE__*/_jsxs("div", {
  children: [/*#__PURE__*/_jsx("div", {}), /*#__PURE__*/_jsx("div", {})]
}));
console.log( /*#__PURE__*/_jsx("div", {
  children: [1, 2].map(i => /*#__PURE__*/_jsx("div", {
    children: i
  }, i))
}));

Actual behavior:

import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
console.log(_jsxs("div", { children: [_jsx("div", {}, void 0)] }, void 0));
console.log(_jsxs("div", { children: [_jsx("div", {}, void 0),
        _jsx("div", {}, void 0)] }, void 0));
console.log(_jsxs("div", { children: [[1, 2].map(i => _jsx("div", { children: i }, i))] }, void 0));
  • On the first line, jsxs is used, and the one child is wrapped in an array, unlike in Babel's output. I believe this will break code that expect a single child node as the children prop.
  • The second line looks good.
  • On the third line, the children are incorrectly wrapped in an array. This can break code expecting children to be an array of jsx nodes, instead I get an extra array wrapping my array of nodes.
  • The void 0 for key-less nodes is also unnecessary.

Playground Link:
https://www.typescriptlang.org/play?ts=4.1.0-insiders.20200925#code/JYWwDg9gTgLgBDAnmApnAVHAhgZzgJRSwGN4AzKCEOAciiNJoG4AoF4iAOxwgBsUAdLwgBzABQs4cADwATYADcAfJKkz5CuAHoVU6Vo0qAlGw7c+g4eNVzFutbc07Vejdvv7DLE+y49+QqISrnYucADeANoAjAA0cABMALoCIFhgYsBwALxK6opwANYoiNnhwAC+SuUVnnZGFTYGoUZAA
I can't configure jsx to be react-jsx in the playground.

Related Issues:

Metadata

Metadata

Assignees

Labels

Fix AvailableA PR has been opened for this issueNeeds InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions