Skip to content
Tutorials Center
My learning

Topics

React Reference ยท JSX Syntax and Rules

The JSX transform

What your compiler turns JSX into: jsx() and jsxs() from react/jsx-runtime versus classic createElement, why no React import is needed, and how to configure it.

Browsers cannot run JSX. A build tool (Babel, SWC, esbuild, the TypeScript compiler) rewrites each tag into an ordinary function call before the code ships. That rewriting step is the JSX transform.

There are two versions. The automatic runtime, used since React 17 and the default in modern tools, compiles tags to jsx() and jsxs() calls imported from react/jsx-runtime, so you never import React just to write JSX. The older classic runtime compiles to React.createElement() and needs React in scope. Both build the same kind of plain object, called a React element.

JSX
// You write
const el = <h1 className="title">Hi</h1>;

// Automatic runtime produces (conceptually)
import { jsx } from 'react/jsx-runtime';
const el = jsx('h1', { className: 'title', children: 'Hi' });

// Classic runtime produces
const el = React.createElement('h1', { className: 'title' }, 'Hi');

Try it

JSX is just function calls
Keys and spread

Syntax

JSX
jsx(type, props, key?)    // one child, or none
jsxs(type, props, key?)   // children is an array of several static children
createElement(type, props, ...children)
Name Description
type A string for an HTML tag, or a component function or object.
props An object; with jsx/jsxs it contains children.
key An optional third argument of jsx/jsxs (a string or number).
children One child in jsx, an array in jsxs; rest arguments in createElement.

Returns: a React element, a plain object with type, props, key and an internal $$typeof marker. It describes what to render; it is not a DOM node.

Configuration: in Babel set runtime: 'automatic' for @babel/preset-react; in TypeScript set "jsx": "react-jsx" (development builds use react-jsxdev); in esbuild jsx: 'automatic'. For the classic mode, "jsx": "react" and React must be imported.

Usage notes

  • jsxs is a hint, not a different result. It marks children that were written statically as an array so React can skip key warnings for them; dynamic arrays go through jsx.
  • No import React is needed with the automatic runtime, and importing it unused triggers lint warnings.
  • key is separated from props. The compiler passes it as the third argument; spreading an object that itself contains a key into JSX (<div {...props} />) makes React warn in development, because the key can no longer be separated at compile time. Write key explicitly.
  • Dev and production differ. react/jsx-dev-runtime adds source locations for better error messages; bundlers switch automatically.
  • JSX is not required. Plain calls to createElement or jsx are fully valid, which helps in tools that cannot compile JSX.
  • Elements are immutable. Do not edit el.props; create a new element with cloneElement or a new call.
  • Debugging. If you see "React is not defined", your tool is still using the classic transform; switch the setting.