- Spec files
npm-react
Describes: pkg:npm/react@18.3.x
- Description
- React is a JavaScript library for building user interfaces with declarative, component-based architecture.
- Author
- tessl
- Last updated
jsx-runtime.md docs/
1# JSX Runtime23The JSX runtime provides low-level functions used by transpilers to convert JSX syntax into JavaScript function calls. These functions are typically not used directly but are important for understanding how JSX works under the hood.45## Capabilities67### jsx (Production Runtime)89Creates React elements from JSX in production mode.1011```javascript { .api }12/**13* Creates React element from JSX (production)14* @param type - Element type (component, HTML tag, etc.)15* @param props - Element props including children16* @param key - Optional element key17* @returns React element18*/19function jsx(type: any, props: any, key?: any): ReactElement;20```2122**Usage Examples:**2324```javascript25// Note: These functions are typically called by transpilers, not directly2627// JSX syntax28const element = <div className="container">Hello World</div>;2930// Transpiles to jsx() call31const element = jsx('div', {32className: 'container',33children: 'Hello World'34});3536// Component JSX37const MyComponent = ({ name }) => <h1>Hello {name}</h1>;38const componentElement = <MyComponent name="John" />;3940// Transpiles to41const componentElement = jsx(MyComponent, { name: 'John' });4243// JSX with key44const listItem = <li key="item-1">First item</li>;4546// Transpiles to47const listItem = jsx('li', { children: 'First item' }, 'item-1');4849// Self-closing JSX50const input = <input type="text" placeholder="Enter name" />;5152// Transpiles to53const input = jsx('input', {54type: 'text',55placeholder: 'Enter name'56});5758// Nested JSX59const nested = (60<div>61<h1>Title</h1>62<p>Content</p>63</div>64);6566// Transpiles to67const nested = jsx('div', {68children: [69jsx('h1', { children: 'Title' }),70jsx('p', { children: 'Content' })71]72});73```7475### jsxs (Production Runtime with Static Children)7677Creates React elements with static children (optimization for multiple children known at compile time).7879```javascript { .api }80/**81* Creates React element with static children (production)82* @param type - Element type (component, HTML tag, etc.)83* @param props - Element props including children array84* @param key - Optional element key85* @returns React element86*/87function jsxs(type: any, props: any, key?: any): ReactElement;88```8990**Usage Examples:**9192```javascript93// JSX with multiple static children94const container = (95<div>96<h1>Title</h1>97<p>Paragraph 1</p>98<p>Paragraph 2</p>99</div>100);101102// Transpiles to jsxs() for static children optimization103const container = jsxs('div', {104children: [105jsx('h1', { children: 'Title' }),106jsx('p', { children: 'Paragraph 1' }),107jsx('p', { children: 'Paragraph 2' })108]109});110111// List with static items112const list = (113<ul>114<li>Item 1</li>115<li>Item 2</li>116<li>Item 3</li>117</ul>118);119120// Transpiles to121const list = jsxs('ul', {122children: [123jsx('li', { children: 'Item 1' }),124jsx('li', { children: 'Item 2' }),125jsx('li', { children: 'Item 3' })126]127});128129// Form with static structure130const form = (131<form>132<label htmlFor="name">Name:</label>133<input id="name" type="text" />134<button type="submit">Submit</button>135</form>136);137138// Transpiles to jsxs() for performance139const form = jsxs('form', {140children: [141jsx('label', { htmlFor: 'name', children: 'Name:' }),142jsx('input', { id: 'name', type: 'text' }),143jsx('button', { type: 'submit', children: 'Submit' })144]145});146```147148### jsxDEV (Development Runtime)149150Creates React elements with additional development-time information for debugging.151152```javascript { .api }153/**154* Creates React element with development info155* @param type - Element type (component, HTML tag, etc.)156* @param props - Element props including children157* @param key - Optional element key158* @param isStaticChildren - Whether children are static159* @param source - Source location information160* @param self - Component instance info161* @returns React element with debug information162*/163function jsxDEV(164type: any,165props: any,166key?: any,167isStaticChildren?: boolean,168source?: any,169self?: any170): ReactElement;171```172173**Usage Examples:**174175```javascript176// In development mode, JSX includes additional debug info177178// JSX in development179const element = <div className="container">Hello World</div>;180181// Transpiles to jsxDEV() with source information182const element = jsxDEV('div', {183className: 'container',184children: 'Hello World'185}, undefined, false, {186fileName: '/src/components/MyComponent.js',187lineNumber: 15,188columnNumber: 10189}, this);190191// Component JSX in development192const MyComponent = ({ name }) => {193return <h1>Hello {name}</h1>;194};195196// Transpiles to jsxDEV() with debugging context197const componentJSX = jsxDEV('h1', {198children: ['Hello ', name]199}, undefined, true, {200fileName: '/src/components/MyComponent.js',201lineNumber: 3,202columnNumber: 10203}, this);204205// Development warnings and errors use this info206function ProblematicComponent() {207// This will show clear error location in dev tools208return (209<div>210<UnknownComponent /> {/* Clear error with file/line info */}211</div>212);213}214```215216### Fragment217218JSX Fragment component available in JSX runtime for grouping elements.219220```javascript { .api }221/**222* Fragment component for JSX runtime223*/224const Fragment: ExoticComponent<{ children?: ReactNode }>;225```226227**Usage Examples:**228229```javascript230import { Fragment } from 'react/jsx-runtime';231232// Fragment in JSX233const elements = (234<>235<h1>Title</h1>236<p>Content</p>237</>238);239240// Transpiles to241const elements = jsx(Fragment, {242children: [243jsx('h1', { children: 'Title' }),244jsx('p', { children: 'Content' })245]246});247248// Fragment with key (useful in lists)249const listItems = items.map(item => (250<Fragment key={item.id}>251<dt>{item.term}</dt>252<dd>{item.definition}</dd>253</Fragment>254));255256// Transpiles to257const listItems = items.map(item =>258jsx(Fragment, {259children: [260jsx('dt', { children: item.term }),261jsx('dd', { children: item.definition })262]263}, item.id)264);265266// Conditional Fragment267function ConditionalContent({ showDetails, data }) {268return (269<div>270<h2>Summary</h2>271{showDetails && (272<>273<p>Details: {data.details}</p>274<p>Updated: {data.lastModified}</p>275</>276)}277</div>278);279}280281// Function returning Fragment282function renderUserInfo(user) {283return (284<>285<span className="user-name">{user.name}</span>286<span className="user-email">({user.email})</span>287</>288);289}290```291292## Transpiler Configuration293294### Automatic JSX Runtime295296Modern React transpiler configuration uses the automatic JSX runtime:297298```javascript299// babel.config.js300{301"presets": [302["@babel/preset-react", {303"runtime": "automatic" // Uses jsx/jsxs imports automatically304}]305]306}307308// webpack.config.js (for esbuild)309{310loader: 'esbuild-loader',311options: {312jsx: 'automatic' // Uses React 17+ JSX transform313}314}315316// tsconfig.json317{318"compilerOptions": {319"jsx": "react-jsx" // TypeScript automatic JSX320}321}322```323324### Classic JSX Runtime (Legacy)325326Older configuration that requires React to be in scope:327328```javascript329// babel.config.js (legacy)330{331"presets": [332["@babel/preset-react", {333"runtime": "classic" // Requires React import334}]335]336}337338// Requires this import in every file with JSX339import React from 'react';340341// JSX transpiles to React.createElement calls342const element = <div>Hello</div>;343// Becomes: React.createElement('div', null, 'Hello');344```345346## Runtime Import Patterns347348```javascript349// Automatic imports (handled by transpiler)350import { jsx, jsxs, Fragment } from 'react/jsx-runtime';351import { jsxDEV, Fragment } from 'react/jsx-dev-runtime';352353// Manual imports (rarely needed)354import { jsx } from 'react/jsx-runtime';355356const manualElement = jsx('div', {357className: 'manual',358children: 'Manually created element'359});360```361362## Types363364```javascript { .api }365// JSX runtime function types366function jsx(type: any, props: any, key?: any): ReactElement;367function jsxs(type: any, props: any, key?: any): ReactElement;368function jsxDEV(369type: any,370props: any,371key?: any,372isStaticChildren?: boolean,373source?: any,374self?: any375): ReactElement;376377// Fragment type378const Fragment: ExoticComponent<{ children?: ReactNode }>;379380// Source information type (development)381interface ReactSource {382fileName: string;383lineNumber: number;384columnNumber: number;385}386387// JSX element type388interface ReactElement<P = any> {389type: any;390props: P;391key: string | number | null;392}393```