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.
// 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
Syntax
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
jsxsis 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 throughjsx.- No
import Reactis needed with the automatic runtime, and importing it unused triggers lint warnings. keyis separated from props. The compiler passes it as the third argument; spreading an object that itself contains akeyinto JSX (<div {...props} />) makes React warn in development, because the key can no longer be separated at compile time. Writekeyexplicitly.- Dev and production differ.
react/jsx-dev-runtimeadds source locations for better error messages; bundlers switch automatically. - JSX is not required. Plain calls to
createElementorjsxare 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.