How Does React actually work ?
The basic concepts of React
- reconciliation
- virtual DOM
- rendering
- diffing algorithm
pre-knowledge
understand the difference between React components, elements and component
What is a React component ?
React component = class or a function that outputs an element tree.
React components also take props as the input
Not only can React elements describe a DOM node(HTML element), they can also describe a component instance.
Not only React call our component for us, it also manages the component instances.
Reconciliation
- All React does is create a tree of elements
- This is very fast, because React elements are just plain JavaScript objects
- This is happening when we call the render() method
- This tree of elements is kept in memory, it is the virtual DOM
- The next thing to do is to sync the virtual DOM with the real DOM
- During the initial render, there is no other way than to insert the full tree
👎Normally: n elements => n^3 operations (not ideal)
React: n elements => n operations
The Diffing algorithm assumptions
- Two elements of different types will produce different trees
- isn't this obvious? Generally speaking not so much but in the context of a React application, this is completely fine
- When we have a list of child elements which often changes, we should provide an unique "key" as a prop
Diffing algorithm operations
Rendering
- Rendering is handled by packages called renderers (React DOM, React Native, etc.)
- React on its own is just a library that allow us to define components, elements and so on + it does the diffing part
- Most of the actual implementation lives in the renderers
- They begin the reconciliation process. They generate the three of elements and insert it wherever it has to be inserted
学而不思则罔,思而不学则殆!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
2023-01-04 可视化Echart模板component