Home Web Front-end JS Tutorial Detailed explanation of elements, components, and nodes in React

Detailed explanation of elements, components, and nodes in React

Mar 01, 2018 am 09:05 AM
react components node

This article mainly introduces the elements, components, and nodes in React to you, and also gives you a reference. I hope it can help you.

Elements, components, instances and nodes in React are four closely related concepts in React, and they are also four concepts that can easily confuse React beginners. Now, veteran cadres will introduce these four concepts in detail, as well as the connections and differences between them, to satisfy the curiosity of students who like to chew words and get to the bottom of things (veteran cadres are one of them).

Element

React element is actually a simple JavaScript object. A React element corresponds to a part of the DOM on the interface, describing the structure and structure of this part of the DOM. Rendering effect. Generally we create React elements through JSX syntax, for example:


const element = <h1 className=&#39;greeting&#39;>Hello, world</h1>;
Copy after login

element is a React element. During the compilation process, the JSX syntax will be compiled into a call to React.createElement(). It can also be seen from the function name that the JSX syntax returns a React element. The compiled result of the above example is:


const element = React.createElement(
 &#39;h1&#39;,
 {className: &#39;greeting&#39;},
 &#39;Hello, world!&#39;
);
Copy after login

Finally, the value of element is a simple JavaScript object similar to the following:


const element = {
 type: &#39;h1&#39;,
 props: {
  className: &#39;greeting&#39;,
  children: &#39;Hello, world&#39;
 }
}
Copy after login

React elements can be divided into two categories: DOM type elements and component type elements. DOM type elements use DOM nodes like h1, p, p, etc. to create React elements. The previous example is a DOM type element; component type elements use React components to create React elements, for example:


const buttonElement = <Button color=&#39;red&#39;>OK</Button>;
Copy after login

buttonElement is a component type element, its value is:


const buttonElement = {
 type: &#39;Button&#39;,
 props: {
  color: &#39;red&#39;,
  children: &#39;OK&#39;
 }
}
Copy after login

For DOM type elements, because it directly corresponds to the DOM node of the page , so React knows how to render. However, for component type elements, such as buttonElement, React cannot directly know what kind of page DOM the buttonElement should be rendered into. In this case, the component itself needs to provide DOM node information that React can recognize. The specific implementation method will be discussed when introducing the component. Detailed introduction.

With the React element, how should we use it? In fact, in most cases, we will not use React elements directly. React will automatically render the final page DOM based on the React elements internally. To be more precise, React elements describe the structure of React's virtual DOM, and React will render the real DOM of the page based on the virtual DOM.

Component (Component)

React component should be the most familiar concept in React. React uses the idea of ​​components to split the interface into reusable modules. Each module is a React component. A React application is composed of several components, and a complex component can also be composed of several simple components.

React components are closely related to React elements. The core function of React components is to return React elements. You may have questions here: shouldn't React elements be returned by React.createElement()? But the call of React.createElement() itself also requires a "person" to be responsible, and the React component is this "responsible person". The React component is responsible for calling React.createElement() and returning the React element for React to internally render it into the final page DOM.

Since the core function of a component is to return React elements, the simplest component is a function that returns React elements:


function Welcome(props) {
 return <h1>Hello, {props.name}</h1>;
}
Copy after login

Welcome is a function that uses defined components. If a component is defined using a class, the work of returning the React element is borne by the render method of the component, for example:


##

class Welcome extends React.Component {
 render() {
  return <h1>Hello, {this.props.name}</h1>;
 }
}
Copy after login

In fact, for components defined using a class, render The method is the only required method. The life cycle methods of other components are just for rendering and are not required.

Now consider the following example:


class Home extends React.Component {
 render() {
  return (
   <p>
    <Welcome name=&#39;老干部&#39; />
    <p>Anything you like</p>
   </p>
  )
 }
}
Copy after login

Home component uses the Welcome component, and the returned React element is:


{
 type: &#39;p&#39;,
 props: {
  children: [
   {
    type: &#39;Welcome&#39;,
    props: {
     name: &#39;老干部&#39;
    }
   },
   {
    type: &#39;p&#39;,
    props: {
     children: &#39;Anything you like&#39;
    }
   },
  ]
 }
}
Copy after login

For this structure, React knows how to render nodes with type = 'p' and type = 'p', but does not know how to render nodes with type = 'Welcome'. When React finds that Welcome is a React component (the judgment is based on the fact that the first letter of Welcome is capitalized), how to render the Welcome node will be determined based on the React element returned by the Welcome component. The React element returned by the Welcome component is:


{
 type: &#39;h1&#39;,
 props: {
  children: &#39;Hello, 老干部&#39;
 }
}
Copy after login

This structure only contains DOM nodes, and React knows how to render. If this structure also contains other component nodes, React will repeat the above process and continue to parse the React elements returned by the corresponding components until the returned React elements only contain DOM nodes. This recursive process allows React to obtain the complete DOM structure information of the page, and the rendering work will naturally come naturally.

In addition, if you think about it carefully, you can find that the reuse of React components is essentially to reuse the React elements returned by this component. React elements are the most basic unit of React applications.

Instance

这里的实例特指React组件的实例。React 组件是一个函数或类,实际工作时,发挥作用的是React 组件的实例对象。只有组件实例化后,每一个组件实例才有了自己的props和state,才持有对它的DOM节点和子组件实例的引用。在传统的面向对象的开发方式中,实例化的工作是由开发者自己手动完成的,但在React中,组件的实例化工作是由React自动完成的,组件实例也是直接由React管理的。换句话说,开发者完全不必关心组件实例的创建、更新和销毁。

节点 (Node)

在使用PropTypes校验组件属性时,有这样一种类型:


MyComponent.propTypes = { 
 optionalNode: PropTypes.node,
}
Copy after login

PropTypes.node又是什么类型呢?这表明optionalNode是一个React 节点。React 节点是指可以被React渲染的数据类型,包括数字、字符串、React 元素,或者是一个包含这些类型数据的数组。例如:


// 数字类型的节点
function MyComponent(props) {
 return 1;
}

// 字符串类型的节点
function MyComponent(props) {
 return &#39;MyComponent&#39;;
}

// React元素类型的节点
function MyComponent(props) {
 return <p>React Element</p>;
}

// 数组类型的节点,数组的元素只能是其他合法的React节点
function MyComponent(props) {
 const element = <p>React Element</p>;
 const arr = [1, &#39;MyComponent&#39;, element];
 return arr;
}

// 错误,不是合法的React节点
function MyComponent(props) {
 const obj = { a : 1}
 return obj;
}
Copy after login

最后总结一下,React 元素和组件的概念最重要,也最容易混淆;React 组件实例的概念大家了解即可,几乎使用不到;React 节点有一定使用场景,但看过本文后应该也就不存在理解问题了。

相关推荐:

React中组件间抽象实例讲解

React中组件的写法有哪些

简单搭建一个react项目

The above is the detailed content of Detailed explanation of elements, components, and nodes in React. 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How to build a real-time chat app with React and WebSocket How to build a real-time chat app with React and WebSocket Sep 26, 2023 pm 07:46 PM

How to build a real-time chat application using React and WebSocket Introduction: With the rapid development of the Internet, real-time communication has attracted more and more attention. Live chat apps have become an integral part of modern social and work life. This article will introduce how to build a simple real-time chat application using React and WebSocket, and provide specific code examples. 1. Technical preparation Before starting to build a real-time chat application, we need to prepare the following technologies and tools: React: one for building

Guide to React front-end and back-end separation: How to achieve decoupling and independent deployment of front-end and back-end Guide to React front-end and back-end separation: How to achieve decoupling and independent deployment of front-end and back-end Sep 28, 2023 am 10:48 AM

React front-end and back-end separation guide: How to achieve front-end and back-end decoupling and independent deployment, specific code examples are required In today's web development environment, front-end and back-end separation has become a trend. By separating front-end and back-end code, development work can be made more flexible, efficient, and facilitate team collaboration. This article will introduce how to use React to achieve front-end and back-end separation, thereby achieving the goals of decoupling and independent deployment. First, we need to understand what front-end and back-end separation is. In the traditional web development model, the front-end and back-end are coupled

How to build simple and easy-to-use web applications with React and Flask How to build simple and easy-to-use web applications with React and Flask Sep 27, 2023 am 11:09 AM

How to use React and Flask to build simple and easy-to-use web applications Introduction: With the development of the Internet, the needs of web applications are becoming more and more diverse and complex. In order to meet user requirements for ease of use and performance, it is becoming increasingly important to use modern technology stacks to build network applications. React and Flask are two very popular frameworks for front-end and back-end development, and they work well together to build simple and easy-to-use web applications. This article will detail how to leverage React and Flask

How to install the Windows 10 old version component DirectPlay How to install the Windows 10 old version component DirectPlay Dec 28, 2023 pm 03:43 PM

Many users always encounter some problems when playing some games on win10, such as screen freezes and blurred screens. At this time, we can solve the problem by turning on the directplay function, and the operation method of the function is also Very simple. How to install directplay, the old component of win10 1. Enter "Control Panel" in the search box and open it 2. Select large icons as the viewing method 3. Find "Programs and Features" 4. Click on the left to enable or turn off win functions 5. Select the old version here Just check the box

How to build a reliable messaging app with React and RabbitMQ How to build a reliable messaging app with React and RabbitMQ Sep 28, 2023 pm 08:24 PM

How to build a reliable messaging application with React and RabbitMQ Introduction: Modern applications need to support reliable messaging to achieve features such as real-time updates and data synchronization. React is a popular JavaScript library for building user interfaces, while RabbitMQ is a reliable messaging middleware. This article will introduce how to combine React and RabbitMQ to build a reliable messaging application, and provide specific code examples. RabbitMQ overview:

React Router User Guide: How to implement front-end routing control React Router User Guide: How to implement front-end routing control Sep 29, 2023 pm 05:45 PM

ReactRouter User Guide: How to Implement Front-End Routing Control With the popularity of single-page applications, front-end routing has become an important part that cannot be ignored. As the most popular routing library in the React ecosystem, ReactRouter provides rich functions and easy-to-use APIs, making the implementation of front-end routing very simple and flexible. This article will introduce how to use ReactRouter and provide some specific code examples. To install ReactRouter first, we need

How to build a fast data analysis application using React and Google BigQuery How to build a fast data analysis application using React and Google BigQuery Sep 26, 2023 pm 06:12 PM

How to use React and Google BigQuery to build fast data analysis applications Introduction: In today's era of information explosion, data analysis has become an indispensable link in various industries. Among them, building fast and efficient data analysis applications has become the goal pursued by many companies and individuals. This article will introduce how to use React and Google BigQuery to build a fast data analysis application, and provide detailed code examples. 1. Overview React is a tool for building

How to build real-time data processing applications using React and Apache Kafka How to build real-time data processing applications using React and Apache Kafka Sep 27, 2023 pm 02:25 PM

How to use React and Apache Kafka to build real-time data processing applications Introduction: With the rise of big data and real-time data processing, building real-time data processing applications has become the pursuit of many developers. The combination of React, a popular front-end framework, and Apache Kafka, a high-performance distributed messaging system, can help us build real-time data processing applications. This article will introduce how to use React and Apache Kafka to build real-time data processing applications, and

See all articles