This article introduces you to the use of React: the five major characteristics of the react framework have certain reference value. Friends in need can refer to it.
Everything in the world has its cause and effect, and the emergence of a framework must be inseparable from a specific era background. For React, there are two main backgrounds that have to be mentioned:
A large amount of business logic has been changed from back-end implementation to front-end implementation: AJAX technology The emergence of the Internet has prompted people to pursue a smoother Web interactive experience, causing a large number of complex business logic to be implemented from back-end development to front-end development. This has caused a geometric increase in the amount of front-end code. Quantitative changes lead to qualitative changes. How to organize and manage this magnitude? front-end code? How to better improve application performance? Drawing on decades of experience in back-end development, the answer, not surprisingly, is to use a large front-end framework.
: Before the advent of React, there were already backbone.js and Angular.js
and other mature large-scale front-end frameworks. However, Facebook engineers found that when faced with complex business scenarios (for example: frequent DOM operations), these frameworks cannot bring good page performance. , so they planned to develop a framework to solve this problem. The framework they developed was React, and the solution to this problem was: use Virtual DOM
.
operation on the DOM tree in a timely manner, we can greatly improve front-end performance. For React, the solution to realize this idea is to use
Virtual DOM. What we call a DOM tree is actually a JavaScript object nested in a tree structure. In the browser, changes to the DOM tree will cause the browser to perform a series of calculations. Therefore, based on the existing DOM tree structure, we can clone an identical DOM tree, that is, a "virtual DOM", and all changes will be included in the browser. It is implemented on this virtual DOM and then merged into the DOM tree in the browser to solve the performance bottleneck mentioned before. Although it is not difficult to understand, this process actually involves many complex situations and some algorithmic difficulties, which is enough to open a new article to explain. I will stop here and stop here. Table.
03. What is componentization?
Componentization has two notable features:
Just as a good function should comply with the "DOT" (Do One Thing) principle, a good React component cannot mix the business logic of other components. We should try to keep React components simple so that complex ones can The internal logic of React components becomes clear.
The following formula can well express the componentization idea of "view is the rendering of data" in React:
UI = render(data)
In this way, in React, the way to form a complex view It becomes very simple:
Combining components;04. Declarative code
Let us briefly compare the difference between "declarative code" and "imperative code":
1. 拿起空调遥控器; 2. 打开空调; 3. 设置温度为 27 摄氏度;
1. 调用“开空调(27)”函数;
, but in essence, this is a better programming thinking, which allows us to no longer just focus on how to implement functions through code , but more thinkingIn order to realize business logic, what functions (functions) are needed by the code, thinking about the design of functions and the relationship between functions make our code logic clearer , rich in order. In React, the data is organized in a tree shape, flowing in one direction from top to bottom (corresponding to the DOM tree). The reason for this design is: The clearer the data flow, the more controllable the status of the component; For single-page applications with complex business logic, the front-end accepts a large amount of data, and the relationship between the data is also It is complicated, and React adopts component development. Different UIs change according to different data. Without a clear and reasonable data flow management method, the final code can only be a mess. Once there is an error in the UI or data, it will be difficult to troubleshoot the error. Where is the source? In view of this, React uses a "one-way data flow" approach that only allows data to flow from parent elements to child elements. The mechanism of React's one-way data flow is roughly as follows: data is stored on the parent component and flows downward to the child component. Although the data is stored on the parent component, both parent and child components can use this data. However, if the data needs to be updated, then only the parent component should be updated. If the child component needs to modify the data, it can only send updated data to the parent component, where the data is actually updated. Once the data is updated by the parent component, the child component will receive the new data. One-way data flow sounds like it adds extra work, but it makes it easier for developers to figure out how the application works. We can easily ignore this feature of React, that is, in React, there is no special proprietary React syntax that needs to be understood and remembered, all Components, data operations, and business logic are all implemented using JavaScript syntax. This means that if you want to use React, you only need to understand the ideas of React and a few key APIs, and you can immediately start using React to develop complex applications. And React also encourages you to use functional programming ideas for development. You can use any of your functional programming skills in React development. In this article we first talked about the background of the React framework, and then explained the five major characteristics of React: Virtual DOM; Componentization; Declarative code; One-way data flow; Pure JavaScript syntax; Related recommendations: 05. One-way data flow
06. Pure JavaScript syntax
07. Summary
The above is the detailed content of Use of React: Five major features of the react framework. For more information, please follow other related articles on the PHP Chinese website!