@babel/preset-react

This preset always includes the following plugins:

And with the development option:

Classic runtime adds:

Automatic runtime (since v7.9.0) adds the functionality for these plugins automatically when the development option is enabled. If you have automatic runtime enabled, adding @babel/plugin-transform-react-jsx-self or @babel/plugin-transform-react-jsx-source will error.

Note: Flow syntax support is no longer enabled in v7. For that, you will need to add the Flow preset.

Installation

You can also check out the React Getting Started page

  • npm
  • Yarn
  • pnpm
  1. npm install --save-dev @babel/preset-react
  1. yarn add --dev @babel/preset-react
  1. pnpm add --save-dev @babel/preset-react

Usage

Without options:

babel.config.json

  1. {
  2. "presets": ["@babel/preset-react"]
  3. }

With options:

babel.config.json

  1. {
  2. "presets": [
  3. [
  4. "@babel/preset-react",
  5. {
  6. "pragma": "dom", // default pragma is React.createElement (only in classic runtime)
  7. "pragmaFrag": "DomFrag", // default is React.Fragment (only in classic runtime)
  8. "throwIfNamespace": false, // defaults to true
  9. "runtime": "classic" // defaults to classic
  10. // "importSource": "custom-jsx-library" // defaults to react (only in automatic runtime)
  11. }
  12. ]
  13. ]
  14. }

Via CLI

Shell

  1. babel --presets @babel/preset-react script.js

Via Node API

JavaScript

  1. require("@babel/core").transformSync("code", {
  2. presets: ["@babel/preset-react"],
  3. });

Options

Both Runtimes

runtime

classic | automatic, defaults to classic

Added in: v7.9.0

Note: The default runtime will be switched to automatic in Babel 8.

Decides which runtime to use.

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

development

boolean, defaults to false.

This toggles behavior specific to development, such as adding __source and __self.

This is useful when combined with the env option configuration or js config files.

throwIfNamespace

boolean, defaults to true.

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

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

pure

boolean, defaults to true.

Enables @babel/plugin-transform-react-pure-annotations. It will mark top-level React method calls as pure for tree shaking.

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. It should be a qualified name (e.g. React.createElement) or an identifier (e.g. createElement).

pragmaFrag

string, defaults to React.Fragment.

Replace the component used when compiling JSX fragments. It should be a valid JSX tag name.

useBuiltIns

boolean, defaults to false.

@babel/preset-react - 图1warning

This option will be removed in Babel 8. Set useBuiltIns to true if you are targeting to modern browsers.

Will use the native built-in instead of trying to polyfill behavior for any plugins that require one.

useSpread

boolean, defaults to false.

Added in: v7.7.0

@babel/preset-react - 图2warning

This option will be removed in Babel 8. Set useSpread to true if you are targeting to modern browsers.

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

babel.config.js

babel.config.js

  1. module.exports = {
  2. presets: [
  3. [
  4. "@babel/preset-react",
  5. {
  6. development: process.env.BABEL_ENV === "development",
  7. },
  8. ],
  9. ],
  10. };

babel.config.json

Note: the env option will likely get deprecated soon

babel.config.json

  1. {
  2. "presets": ["@babel/preset-react"],
  3. "env": {
  4. "development": {
  5. "presets": [["@babel/preset-react", { "development": true }]]
  6. }
  7. }
  8. }

You can read more about configuring preset options here