ReactNative ships with Babel Javascript compiler

Sanjana Human In Tech
3 min readJul 5, 2024

--

Babel Javascript compiler

React Native uses the Babel preset by default when transforming your app’s source code.

Babel is a JavaScript compiler that is commonly used in React Native development to convert modern JavaScript (ES6 and beyond) into a version of JavaScript that can run in current and older browsers or environments.

In the context of React Native, Babel allows developers to use the latest JavaScript features and syntax, as well as JSX syntax, which is used to write React components.

By default React Babel preset plugin is available as devdependencies when React Native project created.

A full list of React Native’s enabled transformations can be found in @react-native/babel-preset.

Key Features of Babel in React Native

  • JavaScript Syntax Transformation: Babel converts new JavaScript syntax (ES6, ES7, etc.) into a backward-compatible version of JavaScript. This includes features like arrow functions, classes, template literals, async/await, and more. Below listed more features of ES6 and ES7.
  • JSX Transformation: React components often use JSX, a syntax extension that allows HTML-like code in JavaScript. Babel transforms JSX into React.createElement calls, which React Native can understand and render.
  • Plugins and Presets: Babel is highly customizable through plugins and presets. Presets are collections of plugins, such as @babel/preset-env for transforming ES6+ syntax and @babel/preset-react for transforming JSX and React-specific syntax.
  • Polyfills : Babel can include polyfills for missing features in older JavaScript environments using @babel/polyfill. This ensures that features like Promises, Object.assign, and others work in environments that don’t support them natively.

How Babel Works in React Native

When you create a React Native project using a tool like expo-cli or react-native-cli, Babel is typically set up for you automatically.

Here's how Babel is integrated and configured in a typical React Native project:

Configuration File: Babel is configured using a .babelrc file or a babel.config.js file in the root of your project. Here’s an example configuration:

The module-react-native-babel-preset is a preset specifically designed for React Native projects, which includes all necessary plugins to transform modern JavaScript and JSX.

Usage in React Native: During development, Babel is invoked by the Metro bundler (React Native’s JavaScript bundler). When you run react-native start, Metro uses Babel to transform your JavaScript and JSX files on-the-fly as you develop.

Example of Babel in Action

Consider the following example of a React component written in modern JavaScript with JSX:

// MyComponent.js
import React from 'react';
import { Text, View } from 'react-native';

class MyComponent extends React.Component {
render() {
return (
<View>
<Text>Hello, Babel!</Text>
</View>
);
}
}

export default MyComponent;

With Babel configured, this code is transformed into a version that can be understood by JavaScript environments that don’t support JSX or the latest JavaScript features:

"use strict";

var _react = _interopRequireDefault(require("react"));
var _reactNative = require("react-native");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

class MyComponent extends _react.default.Component {
render() {
return _react.default.createElement(
_reactNative.View,
null,
_react.default.createElement(
_reactNative.Text,
null,
"Hello, Babel!"
)
);
}
}

module.exports = MyComponent;

Babel is an essential tool in the React Native ecosystem, enabling developers to use modern JavaScript syntax and JSX while ensuring compatibility across different JavaScript environments. By transforming code during the build process, Babel helps maintain a smooth development workflow and leverages the latest language features.

Thank you for reading.

--

--

Sanjana Human In Tech
Sanjana Human In Tech

Written by Sanjana Human In Tech

A React Native front-end enthusiast and dedicated development engineer, eager to expand knowledge on development techniques and collaborate with others.

No responses yet