Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

React Introduction

React Introduction

React is a JavaScript library developed by Facebook (now Meta) and open-sourced in 2013, specifically for building user interfaces, especially single-page applications (SPAs).

React allows developers to use a declarative approach to build reusable UI components.

React's positioning


Core features of React

1. Declarative programming (Declarative)

Traditional imperative programming: telling the computer how to do it

// 原生 JavaScript(命令式)

const button = document.getElementById('myButton');

button.addEventListener('click', function() {

  const counter = document.getElementById('counter');

  const currentValue = parseInt(counter.textContent);

  counter.textContent = currentValue + 1;

});

React declarative programming: telling the computer what result you want

// React(声明式)

function Counter() {

  const [count, setCount] = useState(0);

  return ;

}

Advantage:

2. Componentization (Component-Based)

The core of React is component-based thinking, breaking the UI into independent, reusable, composable components, like building blocks. Each component only cares about its own state (state) and attributes (props), making the logic clear and easy to maintain.

A React application is made up of independent components, each of which:

Example

// A simple component

function

Welcome

(

{

name

}

)

{

 

return

<h1 Hello,{name}>

!

h1

;

}

// Reuse and composition of components

function

App

(

)

{

 

return

(

   

<

div

<

Welcome name

=

"Alice"

/>

     

<

Welcome name

=

"Bob"

/>

     

<

Welcome name

=

"Charlie"

/>

   

div

 

)

;

}

Advantages of componentization:

3. Virtual DOM (Virtual DOM)

What is the Virtual DOM?

The Virtual DOM is a JavaScript object representation of the real DOM, a lightweight in-memory data structure.

How it works:

1. 状态改变

   ↓

2. 生成新的虚拟 DOM 树

   ↓

3. 与旧虚拟 DOM 树进行对比(Diffing)

   ↓

4. 计算出最小差异(Reconciliation)

   ↓

5. 只更新真实 DOM 中变化的部分

Illustration:

Why is the Virtual DOM needed?

Problems with real DOM manipulation:

Advantages of the Virtual DOM:

Example Comparison:

// 假设要更新 1000 个列表项

const items = Array(1000).fill(0).map((_, i) => i);



// Traditional way: operating on the real DOM every time (slow)

items.forEach(item => {

  const li = document.createElement('li');

  li.textContent = item;

  ul.appendChild(li); // 1000 次 DOM 操作

});



// React way: batch updates through virtual DOM

function ItemList() {

  return (

    
    {items.map(item =>
  • {item}
  • )}
); } // React intelligently batches DOM updates

4. Unidirectional data flow (Unidirectional Data Flow)

React's data flows unidirectionally downward, Parent → Child, and Child cannot flow back to Parent:

React uses a top-down data flow approach:

父组件

  ↓ (props)

子组件

  ↓ (props)

孙组件

Feature:

Example

function

Parent

(

)

{

 

const

[

message

,

setMessage

]

=

useState

(

'Hello'

)

;

 

 

return

(

   

<

Child

message

=

{

message

}

// Data flows downward

      onUpdate

=

{

setMessage

}

 

// Communicate upward via callbacks

   

/>

 

)

;

}

function

Child

(

{

message

,

onUpdate

}

)

{

 

return

(

   

<

div

<

p

{

message

}

p

<

button onClick

=

{

(

)

=>

onUpdate

(

'Updated!'

)

}

        Update

     

button

   

div

 

)

;

}


Main advantages of React

1. Relatively gentle learning curve

2. A powerful ecosystem

3. Broad community support

4. Excellent performance

5. Cross-platform capability

6. Enterprise-level application support

Used by many well-known companies:


When to use React

Scenarios suitable for React:

Scenarios less suitable for React:


React compared with other frameworks

Feature React Vue Angular
Type library Progressive framework Complete framework
Learning curve Medium Gentler Steeper
Flexibility High Medium Low (many conventions)
Ecosystem Very rich Rich Full integration
Enterprise support Meta independent Google
TypeScript Well supported Well supported Native support

The development history of React