search
HomeWeb Front-endJS TutorialUse of react: How React renders UI

Use of react: How React renders UI

Jul 20, 2018 am 10:37 AM
javascriptreact.js

This article introduces to you the use of react: How React renders UI has a certain reference value. Friends in need can refer to it.

01. The way React renders the interface

Before the emergence of large front-end frameworks such as React, the way we rendered UI elements was to use String templates. In React, we render UI elements by using JavaScript objects.

As we mentioned in the previous chapter, React proposed the concept of virtual DOM in order to save the front-end performance consumed by frequent DOM operations. The JavaScript object we create here is Virtual DOM node used to describe "what the page looks like". How is "virtual DOM" finally transformed into "real DOM" and displayed in the browser? The complex work here (manipulating the DOM tree, adding nodes) is done by React.

Let us first look at how to create a virtual DOM node (ie React element) through a JavaScript object:

// 为了创建一个 React 元素,我们需要使用 React.createElement API
const element = React.createElement(
  'h1',
  {className: 'greeting'},
  'Hello, world!'
);

The API will eventually return a JavaScript object in the following format:

const element = {
  type: 'h1',
  props: {
    className: 'greeting',
    children: 'Hello, world'
  }
};

React will find a place for this JavaScript object in the generated virtual DOM tree, and eventually merge it with the real DOM tree in the browser to render the view.

However, in actual development, you will rarely use the React.createElement API, but create React elements as follows:

const element = (
  <h1>
    Hello, world!
  </h1>
);

This creation method is It is implemented through a JavaScript syntax extension called JSX. I will not elaborate further on the concept of JSX here. You can understand it as a kind of simplicity. Syntactic sugar for efficiently creating React elements, used to build the virtual DOM of the entire application more elegantly.

It is worth mentioning that JSX is not part of the React framework (this stems from the philosophy of divide and conquer as much as possible in React code organization), so React is not responsible for merging virtual DOM and real Like the DOM, it is responsible for converting code written in JSX syntax into JavaScript objects using the React.createElement API.

Then who will do this? The answer is Babel. Usually, we use webpack to package our JavaScript code and send it to Babel for translation. Now you understand why React, webpack and Babel always appear together like conjoined twins.

So far, we already know how to create React elements, but in fact we just "Create", before the element is actually displayed on the browser, we also check the key One step "rendering".

Here we speed up. If we want to render the previously created React element, we need to use the following code:

<p></p>

const element = <h1 id="Hello-world">Hello, world</h1>;

// 使用 ReactDOM.render API
ReactDOM.render(
  element,
  document.getElementById('root')
);

Yes, the id is root The DOM element will become the root node of the entire virtual DOM tree. So far, we have mastered the entire process of converting React elements into virtual DOM nodes and then rendering the elements on the browser. However, just being able to use React to render visual elements is far from realizing the value of React. Don’t forget that React exists as a large-scale front-end framework (although compared to other large-scale front-end frameworks, its components are not complete). The real value of React is: Use React elements to implement various complex tasks concisely and efficiently Business logic.

How to do this? The answer is to use React components.

02. React components

React components not only give us the ability topackage a bunch of visual elementsbut also give us the ability to packagea series of corresponding Interaction behavior. It can be said: React components are the cornerstone of React applications.

So what is a React component? You can imagine that a React component is like a factory, which receives a series of materials called properties, and ultimately produces (returns) React elements/components.

Let’s put it another way, a React component is essentially a JavaScript function that receives a series of parameters and returns a React element/component. Let's see how it is written:

import React form 'react'
import ReactDOM form 'react-dom'

function Button(props) {
    return <button>{props.buttonName}</button>
}

See, React components fully comply with the componentization idea we mentioned before, receiving parameters and returning UI elements.

It’s a great idea to think about building React applications from a component perspective, because componentization means modularity and reusability. Component classes are like instances of a factory producing components. These component classes fully comply with the "Single Response Principle" and "DOT" principles.

In React’s official documentation, a large number of React APIs are about components. Therefore, components are a very important concept in React. In essence, components are the main encapsulation unit given to us by React. Through components, we can quickly build a large application with complex interaction logic and visual interface like building blocks, and each visual unit in the application has very clear responsibilities.

Hopefully by this point you can appreciate the value of React when building large applications. It allows us to focus on a small part of the application without inadvertently affecting the rest of the application (i.e. each component In line with the principle of "high cohesion, low coupling"). Using React, it is easier for us to write clear and elegant code.

03. Summary

Finally, let us once again summarize the two advantages of using components to render interfaces in React:

  1. Easy to reuse : We can call a component at any time and place;

  2. Convenient customization: By giving different attributes to the component, we can get different UI elements;

Related recommendations:

Use of React: Five major features of the react framework

The above is the detailed content of Use of react: How React renders UI. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

The Origins of JavaScript: Exploring Its Implementation LanguageThe Origins of JavaScript: Exploring Its Implementation LanguageApr 29, 2025 am 12:51 AM

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

Behind the Scenes: What Language Powers JavaScript?Behind the Scenes: What Language Powers JavaScript?Apr 28, 2025 am 12:01 AM

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

See all articles

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

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool