DEV Community

ibrahim wael ibrahim fouad
ibrahim wael ibrahim fouad

Posted on

Understanding How React Works

What is React? ⚛️

React is a JavaScript library that helps us build web applications faster and more efficiently. It allows for better handling of data and works using a Single Page Application (SPA) model. While this is a concise definition, there's another one I find more comprehensive:

"React is a very popular JavaScript library that uses components and state to build user interfaces (UI). It was developed by Facebook."

- Jonas Schmedtmann

Note: From here on, I'll assume you're somewhat familiar with React or have worked with it before.


How Does React Work?

React is entirely based on the concept of components. A component is a smaller part of the UI that can be reused. Now, let’s walk through the process of how the code turns into the UI you see on the screen.

ComponentComponent InstancesReact ElementsDOM ElementsUI on the Screen

So now that you understand what a component is, let’s dive into the component instances, which are the actual working versions of components. Essentially, a component is like a blueprint or a template. When we create component instances, we pass props and assign specific states to them. These instances are what we work with in React.

If you want to dive deeper into this concept, I recommend searching for "React Components, Instances, and Elements" on Medium.

Now, my goal is to explain the process that happens next:

React ElementsDOM ElementsUI on the Screen

This process works in four phases, which we'll explore one by one:

  1. Render is Triggered
  2. Render Phase
  3. Commit Phase
  4. Browser Paint

1. Render is Triggered

Rendering can be triggered in two cases:

  • Initial Render: This happens when the app is loaded for the first time.
  • State Update (Re-render): This occurs when there's a change in the application, usually when the state changes.

Not much happens in this phase, so let's move on to the next one.


2. Render Phase

Now, let’s redefine what “render” means. As front-end developers, we often use the term render to describe a change in the UI. But that's not entirely accurate. Rendering in React means preparing the UI for the next phase.

The render phase involves several steps:

Step 1: Component Instances That Triggered Re-render

This is when React detects that a change has occurred in the component.

Step 2: React Elements

This step involves gathering all the components in the project and constructing what's called a Virtual DOM Tree. This tree outlines the relationship between components as nodes, where each parent component has its child components.

Here’s an important point: when a parent component changes, all of its children will also re-render.


In this stage, several things happen simultaneously:

  • First, React creates the virtual DOM tree.
  • Next, the Reconciler steps in. This is responsible for identifying what has changed. In React, the reconciler is called Fiber.

If you want to explore Fiber in detail, you can check out:

  • "What is React Fiber and How It Helps You Build a High-Performing React Application" on Medium.
  • "Building High-Performance Applications with React Fiber" on Dhiwise.

What is Fiber?

Fiber is the engine responsible for coordinating the changes in components. It tells us what changed, what was removed, and what was added. This is a simplified explanation, but Fiber involves a few key concepts:

  • Reconciliation: Identifies the changes (added, updated, or removed DOM elements).
  • Diffing: A highly optimized algorithm that compares the current DOM with the virtual DOM. It operates based on two rules:
    1. If the type of DOM element changes.
    2. If attributes or props change.

This algorithm speeds up operations from O(n³) to O(n), improving app performance.

  • Fiber Tree: This tree gets created during the Initial Render. It mirrors the virtual DOM tree, but with differences. It provides insight into:
    • Current State
    • Props
    • Side Effects
    • Used Hooks
    • Work Queue

The Render Phase is asynchronous, meaning it can be paused, resumed, canceled, or prioritized depending on necessity. This allows React to update the Fiber Tree continuously as the app runs.

The end result of this phase is a List of Effects, which is passed to the next phase.


3. Commit Phase

In this phase, React takes the List of Effects generated in the render phase and applies these changes to the DOM. This is handled by another library, ReactDOM. ReactDOM is responsible for manipulating the actual DOM based on the changes React identified.

Important to note: while React is responsible for creating the UI structure, ReactDOM handles browser-specific operations. This means that for different platforms (e.g., React Native), a different library like React Native manages how UI is rendered.

The commit phase must be synchronous because we can’t partially render our interface. The changes need to be applied all at once.


4. Browser Paint

This final step is outside of React's scope. It involves the browser's rendering engine painting the changes to the screen. Each browser handles this differently, and it’s worth looking into browser-specific rendering processes.

If you’re interested in how browsers work, check out:

  • "Populating the Page: How Browsers Work" on MDN.
  • "Render-tree Construction, Layout, and Paint" on web.dev.
  • "The Anatomy of Browser Rendering: How Web Pages Come to Life" on Medium.
  • "What is the DOM & How Does HTML Rendering Happen in Browsers" on Medium.

Further Reading:

If you're interested in diving deeper, here are some useful resources:

  • "react-fiber-architecture (acdlite)" on GitHub.
  • "render-and-commit" on React.dev.
  • The Ultimate React Course 2024: React, Next.js, Redux & More by Jonas Schmedtmann.

Top comments (1)

Collapse
 
mousa profile image
Reyad

Great ❤️❤️