Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

TypeScript rejects a component that returns an array or other non-Element

Error'List' cannot be used as a JSX component.

Short answer

TypeScript before 5.1 required every component to return JSX.Element or null, so arrays, strings and Promises were rejected even though React renders them. Upgrade TypeScript to 5.1 or newer, together with a current @types/react, or wrap the returned array in a Fragment so the component returns one element.

  • Applies to TypeScript 5.5, React 19, @types/react 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check the TypeScript version the project and the editor use. The check changed in 5.1, so an older compiler explains the error.

    npx tsc --version && npm ls typescript @types/react
  2. 2Match what you see, then jump to its fix

    1. 1The error appears for components that React itself renders fine, such as ones returning an arrayTypeScript is older than 5.1Fix
    2. 2Its return type 'Element[]' is not a valid JSX elementThe component returns an array and you cannot upgrade yetFix

What does the error look like?

  • 'List' cannot be used as a JSX component.
  • Its return type 'Element[]' is not a valid JSX element.
  • Its return type 'Promise<Element>' is not a valid JSX element.

Cause 1 of 2

TypeScript is older than 5.1

1Check

Before 5.1 every JSX tag had to be compatible with JSX.Element. Version 5.1 looks up JSX.ElementType instead, which lets the types package say what is valid as a tag.

npx tsc --version

2Fix

Upgrade TypeScript, and make sure the editor uses the workspace copy and not a bundled older one.

npm install --save-dev typescript@latest

3Verify

Run the type check; the error disappears for the same file.

npx tsc --noEmit

Confirmed by typescriptlang.org

Did this fix it?

Cause 2 of 2

The component returns an array and you cannot upgrade yet

1Check

Find the component whose body returns items.map(...) directly.

grep -rn "return .*\.map(" src/

2Fix

Wrap the result in a Fragment. A Fragment groups elements into one value and adds no DOM node. When each item is itself a Fragment, use the explicit Fragment tag with a key.

function List({ items }: { items: Item[] }) {
  return (
    <>
      {items.map(i => <Row key={i.id} item={i} />)}
    </>
  );
}

3Verify

Run the type check, then render the component in the browser.

npx tsc --noEmit

Confirmed by react.dev

Did this fix it?

How do I stop it coming back?

  • Keep TypeScript and @types/react on current versions, and update them together.
  • Install one copy of @types/react only; check with npm ls @types/react.
  • Have the editor use the workspace TypeScript version.
  • Return a Fragment when a component renders a list at its top level.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

TypeScript rejects MUI Data Grid columns that set a type

A TypeScript error inside a column's renderCell callback

A columns array declared without a type is inferred with type as a plain string, which the Data Grid's column type union does not accept.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.