Overview of React

Yuvaraj S
5 min readJust now

--

React has revolutionized the way we build user interfaces, making it easier and more efficient to develop dynamic web applications. Two key concepts that play crucial roles in React’s functionality are the Virtual DOM and Babel.

What is React?

React is a popular JavaScript library for building user interfaces, particularly single-page applications where you need a fast and interactive user experience. Developed and maintained by Facebook, React allows developers to create large web applications that can change data without reloading the page. Its main goal is to be fast, scalable, and simple. React focuses on building reusable UI components, which can manage their own state. By breaking the UI into smaller, manageable pieces.

In this blog we will be covering
— Setting Up a React Project
— Understanding the Virtual DOM
— Babel
— Code Example

Setting Up a React Project

Before diving into the Virtual DOM and Babel, let’s set up a React project. We’ll use Create React App, a popular toolchain for creating React applications.

Step 1: Install Node.js

First, make sure you have Node.js installed on your computer. You can download it from nodejs.org.

Step 2: Create a React App

Open your terminal or command prompt and run the following command to create a new React application:

npx create-react-app my-app

This command will create a new directory called my-app with all the necessary files and dependencies.

Step 3: Navigate to Your App Directory

cd my-app

Step 4: Start the Development Server

npm start

This command will start the development server and open your new React application in the browser.

Understanding the Virtual DOM

What is the DOM?

The Document Object Model (DOM) is a programming interface for web documents. It represents the structure of a webpage, allowing programs to manipulate the content, structure, and style of a website dynamically. The DOM is essential for making web pages interactive, but it can be slow and inefficient when dealing with large or complex applications.

Enter the Virtual DOM

The Virtual DOM is an abstraction of the real DOM. It is a lightweight copy that React keeps in memory, which allows for more efficient updates and rendering. Here’s how it works:

  1. Initial Render: When a React component is first rendered, a virtual representation of the DOM is created.
    (ie, you have loaded one api and ran a *FOR LOOP*)
  2. Updates: When the state or props of a component change, a new virtual DOM is created.
    (ie, Each time you update in your code, it won’t refresh. During Development)
  3. Batch Updates: Instead of updating the real DOM immediately, React batches these updates together.
  4. Efficient Rendering: Only the parts of the DOM that have actually changed are updated, minimizing the number of manipulations on the real DOM and improving performance.
    (ie, Whatever changed, only that will be rendered)

By using the Virtual DOM, React can make updates more efficiently, which leads to smoother and faster user experiences.

Babel

What is Babel?

Babel is a JavaScript compiler that allows developers to use the latest JavaScript features, even if they are not yet supported by all browsers. It translates modern JavaScript (ES6 and beyond) into a version that current browsers can understand.

Why Use Babel with React?

React applications often use modern JavaScript syntax and JSX (JavaScript XML), which is a syntax extension that allows HTML to be written within JavaScript. Browsers do not natively understand JSX or some of the newer JavaScript features, so Babel is used to transform this code into a form that browsers can execute.

Key Features of Babel

  1. Transpilation: Converts ES6+ code into ES5, ensuring compatibility with older browsers.
  2. JSX Transformation: Transforms JSX syntax into regular JavaScript function calls.
  3. Plugins and Presets: Babel is highly configurable, with a rich ecosystem of plugins and presets that allow developers to tailor the compilation process to their needs.

How Virtual DOM and Babel Work Together in React

When developing a React application, the workflow generally involves writing components in JSX and modern JavaScript. Here’s how the Virtual DOM and Babel work together in this process:

  1. Writing Code: Developers write components using JSX and modern JavaScript features.
  2. Compilation with Babel: Babel compiles this code into standard JavaScript that browsers can understand. This includes transforming JSX into React.createElement calls and converting newer JavaScript syntax into ES5.
  3. Virtual DOM Management: React uses the compiled code to manage the Virtual DOM. When state or props change, React creates a new Virtual DOM and efficiently updates the real DOM based on the differences.
  4. Efficient Updates: The Virtual DOM ensures that only the necessary parts of the real DOM are updated, leading to better performance.

Code Example: Simple React Component

In this example, we’ll create a simple React component that displays a counter and a button to increment it. We’ll write the component using JSX and modern JavaScript, and then see how Babel transforms it and how the Virtual DOM handles updates.

Step 1: Writing the Component

Create a new file Counter.js in the src directory with the following content:

import React, { useState } from 'react';
const Counter = () => {
const [count, setCount] = useState(0);
const increment = () => {
setCount(count + 1);
};
return (
<div>
<p>Current Count: {count}</p>
<button onClick={increment}>Increment</button>
</div>
);
};
export default Counter;

In this code:

  • We use the useState hook to create a state variable count and a function setCount to update it.
  • The increment function updates the count state by incrementing it by 1.
  • The component renders a paragraph displaying the current count and a button to increment it.

Step 2: Using the Component

Update the App.js file to use the Counter component:

// src/App.js
import React from 'react';
import Counter from './Counter';

function App() {
return (
<div className="App">
<h1>My Counter App</h1>
<Counter />
</div>
);
}
export default App;

Now, when you run your app (npm start), you will see the counter and the button on the screen. Clicking the button will increment the counter.

Step 3: Babel Transpilation

Babel transforms the above JSX and modern JavaScript code into a format that browsers can understand. Here’s an example of what the compiled code might look like:

var _react = _interopRequireWildcard(require("react"));function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } }var Counter = function Counter() {
var _useState = (0, _react.useState)(0),
count = _useState[0],
setCount = _useState[1];
var increment = function increment() {
setCount(count + 1);
};
return _react.default.createElement(
"div",
null,
_react.default.createElement(
"p",
null,
"Current Count: ",
count
),
_react.default.createElement(
"button",
{ onClick: increment },
"Increment"
)
);
};
exports.default = Counter;

In the compiled code:

  • JSX is transformed into React.createElement calls.
  • Modern JavaScript syntax is converted into ES5 to ensure compatibility with older browsers.

Conclusion

The Virtual DOM and Babel are fundamental to the efficiency and flexibility of React applications. The Virtual DOM allows for fast and efficient updates, making dynamic user interfaces smoother. Babel enables developers to write modern JavaScript and JSX, ensuring that their code works across all browsers. Together, they form a powerful combination that enhances the React development experience.

Understanding these concepts is crucial for any React developer also before starting having this overview is very important, as they underpin many of the optimizations and features that make React such a popular and effective library for building user interfaces.

--

--

Yuvaraj S

Passionate Angular Developer. I post programming content regarding, Frontend Development with working example. ie, Angular, Html, CSS, Javascript