@babel/preset-react

This preset always includes the following plugins:

And with the development option:

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

  1. npm install --save-dev @babel/preset-react

Usage

.babelrc

Without options:

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

With options:

  1. {
  2. "presets": [
  3. [
  4. "@babel/preset-react",
  5. {
  6. "pragma": "dom", // default pragma is React.createElement
  7. "pragmaFrag": "DomFrag", // default is React.Fragment
  8. "throwIfNamespace": false // defaults to true
  9. }
  10. ]
  11. ]
  12. }

Via CLI

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

Via Node API

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

Options

pragma

string, defaults to React.createElement.

Replace the function used when compiling JSX expressions.

pragmaFrag

string, defaults to React.Fragment.

Replace the component used when compiling JSX fragments.

useBuiltIns

boolean, defaults to false.

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

useSpread

boolean, defaults to false.

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

development

boolean, defaults to false.

Toggles plugins that aid in development, such as @babel/plugin-transform-react-jsx-self and @babel/plugin-transform-react-jsx-source.

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:

  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.

.babelrc.js

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

.babelrc

Note: the env option will likely get deprecated soon

  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