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.
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.
Component → Component Instances → React Elements → DOM Elements → UI 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 Elements → DOM Elements → UI on the Screen
This process works in four phases, which we'll explore one by one:
Rendering can be triggered in two cases:
Not much happens in this phase, so let's move on to the next one.
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:
This is when React detects that a change has occurred in the component.
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:
If you want to explore Fiber in detail, you can check out:
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:
This algorithm speeds up operations from O(n³) to O(n), improving app performance.
渲染阶段是异步的,这意味着它可以根据需要暂停、恢复、取消或确定优先级。这允许 React 在应用程序运行时不断更新 Fiber Tree。
此阶段的最终结果是效果列表,它将传递到下一阶段。
在此阶段,React 获取在渲染阶段生成的 效果列表 并将这些更改应用到 DOM。这是由另一个库 ReactDOM 处理的。 ReactDOM 负责根据 React 识别的更改来操作实际 DOM。
重要注意事项:React 负责创建 UI 结构,而 ReactDOM 则处理特定于浏览器的操作。这意味着对于不同的平台(例如 React Native),不同的库(例如 React Native 管理 UI 的渲染方式。
提交阶段必须同步,因为我们无法部分渲染我们的界面。更改需要立即应用。
这最后一步超出了 React 的范围。它涉及浏览器的渲染引擎将更改绘制到屏幕上。每个浏览器对此的处理方式不同,值得研究特定于浏览器的渲染过程。
如果您对浏览器的工作原理感兴趣,请查看:
如果您有兴趣深入了解,这里有一些有用的资源:
The above is the detailed content of Understanding How React Works. For more information, please follow other related articles on the PHP Chinese website!