Home > Web Front-end > JS Tutorial > Understanding How React Works

Understanding How React Works

Linda Hamilton
Release: 2024-09-30 20:28:29
Original
747 people have browsed it

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.

  • 纤维树:此树是在初始渲染期间创建的。它镜像虚拟 DOM 树,但有所不同。它提供了以下方面的见解:
    • 当前状态
    • 道具
    • 副作用
    • 用过的挂钩
    • 工作队列

渲染阶段是异步的,这意味着它可以根据需要暂停、恢复、取消或确定优先级。这允许 React 在应用程序运行时不断更新 Fiber Tree

此阶段的最终结果是效果列表,它将传递到下一阶段。


3. 提交阶段

在此阶段,React 获取在渲染阶段生成的 效果列表 并将这些更改应用到 DOM。这是由另一个库 ReactDOM 处理的。 ReactDOM 负责根据 React 识别的更改来操作实际 DOM。

重要注意事项:React 负责创建 UI 结构,而 ReactDOM 则处理特定于浏览器的操作。这意味着对于不同的平台(例如 React Native),不同的库(例如 React Native 管理 UI 的渲染方式。

提交阶段必须同步,因为我们无法部分渲染我们的界面。更改需要立即应用。


4. 浏览器画图

这最后一步超出了 React 的范围。它涉及浏览器的渲染引擎将更改绘制到屏幕上。每个浏览器对此的处理方式不同,值得研究特定于浏览器的渲染过程。

如果您对浏览器的工作原理感兴趣,请查看:

  • MDN 上的“填充页面:浏览器如何工作”
  • web.dev 上的“渲染树构建、布局和绘制”
  • Medium 上的“浏览器渲染剖析:网页如何变得栩栩如生”
  • Medium 上的“什么是 DOM 以及 HTML 渲染如何在浏览器中发生”

进一步阅读:

如果您有兴趣深入了解,这里有一些有用的资源:

  • GitHub 上的“react-fibre-architecture (acdlite)”
  • React.dev 上的“渲染并提交”
  • 2024 年终极 React 课程:React、Next.js、Redux 等 作者:Jonas Schmedtmann。

The above is the detailed content of Understanding How React Works. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template