Home Web Front-end JS Tutorial Understanding How React Works

Understanding How React Works

Sep 30, 2024 pm 08:28 PM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

The Evolution of JavaScript: Current Trends and Future Prospects The Evolution of JavaScript: Current Trends and Future Prospects Apr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript Engines: Comparing Implementations JavaScript Engines: Comparing Implementations Apr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript: Exploring the Versatility of a Web Language JavaScript: Exploring the Versatility of a Web Language Apr 11, 2025 am 12:01 AM

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration) How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration) Apr 11, 2025 am 08:22 AM

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

From C/C   to JavaScript: How It All Works From C/C to JavaScript: How It All Works Apr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Building a Multi-Tenant SaaS Application with Next.js (Backend Integration) Building a Multi-Tenant SaaS Application with Next.js (Backend Integration) Apr 11, 2025 am 08:23 AM

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

See all articles