@babel/plugin-transform-react-jsx

    Automatic runtime is a feature added in v7.9.0. With this runtime enabled, the functions that JSX compiles to will be imported automatically.

    In

    Out

    1. import { jsx as _jsx } from "react/jsx-runtime";
    2. import { jsxs as _jsxs } from "react/jsx-runtime";
    3. const profile = _jsxs("div", {
    4. children: [
    5. _jsx("img", {
    6. src: "avatar.png",
    7. className: "profile",
    8. }),
    9. _jsx("h3", {
    10. children: [user.firstName, user.lastName].join(" "),
    11. }),
    12. ],
    13. });

    Customizing the Automatic Runtime Import

    In

    1. /** @jsxImportSource custom-jsx-library */
    2. const profile = (
    3. <div>
    4. <img src="avatar.png" className="profile" />
    5. <h3>{[user.firstName, user.lastName].join(" ")}</h3>
    6. </div>
    7. );

    Out

    1. import { jsx as _jsx } from "custom-jsx-library/jsx-runtime";
    2. const profile = _jsxs("div", {
    3. children: [
    4. _jsx("img", {
    5. src: "avatar.png",
    6. className: "profile",
    7. }),
    8. _jsx("h3", {
    9. children: [user.firstName, user.lastName].join(" "),
    10. }),
    11. ],
    12. });

    In

    1. /** @jsxRuntime classic */
    2. const profile = (
    3. <div>
    4. <img src="avatar.png" className="profile" />
    5. <h3>{[user.firstName, user.lastName].join(" ")}</h3>
    6. </div>
    7. );

    Out

    1. "div",
    2. null,
    3. React.createElement("img", { src: "avatar.png", className: "profile" }),
    4. React.createElement("h3", null, [user.firstName, user.lastName].join(" "))
    5. );

    React Classic Runtime

    If you do not want (or cannot use) auto importing, you can use the classic runtime, which is the default behavior for v7 and prior.

    In

    Out

    1. const profile = React.createElement(
    2. "div",
    3. null,
    4. React.createElement("img", { src: "avatar.png", className: "profile" }),
    5. React.createElement("h3", null, [user.firstName, user.lastName].join(" "))
    6. );

    Customizing the Classic Runtime Import

    In

    1. /** @jsx Preact.h */
    2. import Preact from "preact";
    3. const profile = (
    4. <div>
    5. <img src="avatar.png" className="profile" />
    6. <h3>{[user.firstName, user.lastName].join(" ")}</h3>
    7. </div>
    8. );

    Out

    1. /** @jsx Preact.h */
    2. import Preact from "preact";
    3. const profile = Preact.h(
    4. null,
    5. Preact.h("img", { src: "avatar.png", className: "profile" }),
    6. Preact.h("h3", null, [user.firstName, user.lastName].join(" "))
    7. );

    Fragments

    React

    In

    1. const descriptions = items.map(item => (
    2. <>
    3. <dt>{item.name}</dt>
    4. <dd>{item.value}</dd>
    5. </>
    6. ));

    Out

    1. const descriptions = items.map(item =>
    2. React.createElement(
    3. React.Fragment,
    4. null,
    5. React.createElement("dt", null, item.name),
    6. React.createElement("dd", null, item.value)
    7. )
    8. );

    Customizing with the Classic Runtime

    In

    Out

    1. /** @jsx Preact.h */
    2. /** @jsxFrag Preact.Fragment */
    3. import Preact from "preact";
    4. var descriptions = items.map(item =>
    5. Preact.Fragment,
    6. null,
    7. Preact.h("dt", null, item.name),
    8. Preact.h("dd", null, item.value)
    9. )
    10. );

    Note that if a custom pragma is specified, then a custom fragment pragma must also be specified if the fragment sytnax <></> is used. Otherwise, an error will be thrown.

    1. npm install --save-dev @babel/plugin-transform-react-jsx

    Without options:

    1. {
    2. "plugins": ["@babel/plugin-transform-react-jsx"]
    3. }

    With options:

    1. {
    2. "plugins": [
    3. [
    4. "@babel/plugin-transform-react-jsx",
    5. {
    6. "throwIfNamespace": false, // defaults to true
    7. "runtime": "automatic", // defaults to classic
    8. "importSource": "custom-jsx-library" // defaults to react
    9. }
    10. ]
    11. ]
    12. }

    Via CLI

      Via Node API

      throwIfNamespace

      boolean, defaults to true.

      Toggles whether or not to throw an error if an XML namespaced tag name is used. For example:

      1. <f:image />

      Though the JSX spec allows this, it is disabled by default since React’s JSX does not currently have support for it.

      You can read more about configuring plugin options

      runtime

      classic | automatic, defaults to classic

      Decides which runtime to use.

      automatic auto imports the functions that JSX transpiles to. classic does not automatically import anything.

      React Automatic Runtime

      importSource

      string, defaults to react.

      Added in: v7.9.0

      Replaces the import source when importing functions.

      React Classic Runtime

      pragma

      string, defaults to React.createElement.

      Replace the function used when compiling JSX expressions.

      Note that the @jsx React.DOM pragma has been deprecated as of React v0.12

      pragmaFrag

      string, defaults to React.Fragment.

      Replace the component used when compiling JSX fragments.

      boolean, defaults to false.

      When spreading props, use Object.assign directly instead of Babel’s extend helper.

      useSpread

      When spreading props, use inline object with spread elements directly instead of Babel’s extend helper or .