Home Web Front-end Front-end Q&A What do the three dots represent in react?

What do the three dots represent in react?

Dec 27, 2022 pm 04:37 PM
react

The three dots in React represent the "extension operator"; in React, the extension operator is generally used for batch assignment of attributes, such as "var props = {};props.foo = x; props.bar = y;var component = <Component {...props} />;".

What do the three dots represent in react?

The operating environment of this tutorial: Windows 10 system, react version 18.0.0, Dell G3 computer.

react What do the three dots represent?

represents the "extension operator".

The ... operator (also called the spread operator) is already supported by ES6 arrays. It allows passing arrays or array-like functions directly as parameters of functions without going through apply.

var people=[&#39;Wayou&#39;,&#39;John&#39;,&#39;Sherlock&#39;];
//sayHello函数本来接收三个单独的参数人一,人二和人三
function sayHello(people1,people2,people3){
    console.log(`Hello ${people1},${people2},${people3}`);
}
//但是我们将一个数组以拓展参数的形式传递,它能很好地映射到每个单独的参数
sayHello(...people);//输出:Hello Wayou,John,Sherlock 
//而在以前,如果需要传递数组当参数,我们需要使用函数的apply方法
sayHello.apply(null,people);//输出:Hello Wayou,John,Sherlock
Copy after login

In React, the extension operator is generally used for batch assignment of attributes. In JSX, you can use the ... operator to merge the key-value pair of an object with the props attribute of ReactElement.

var props = {};
  props.foo = x;
  props.bar = y;
  var component = &lt;Component {...props} /&gt;;
  
//等价于
var props = {};
  props.foo = x;
  props.bar = y;
  var component = &lt;Component foo={x} bar={y} /&gt;;
Copy after login

It can also be mixed with ordinary XML attributes. It requires an attribute with the same name, and the latter will override the former:

var props = { foo: &#39;default&#39; };
var component = &lt;Component {...props} foo={&#39;override&#39;} /&gt;;
console.log(component.props.foo); // &#39;override&#39;
Copy after login

Recommended learning: "react video tutorial"

The above is the detailed content of What do the three dots represent 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 Article Tags

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 app with React and WebSocket

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

Guide to React front-end and back-end separation: How to achieve decoupling and independent deployment of front-end and back-end

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 build simple and easy-to-use web applications with React and Flask

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 app with React and RabbitMQ

React code debugging guide: How to quickly locate and solve front-end bugs React code debugging guide: How to quickly locate and solve front-end bugs Sep 26, 2023 pm 02:25 PM

React code debugging guide: How to quickly locate and solve front-end bugs

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 build a fast data analysis application using React and Google BigQuery

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

React Router User Guide: How to implement front-end routing control

React responsive design guide: How to achieve adaptive front-end layout effects React responsive design guide: How to achieve adaptive front-end layout effects Sep 26, 2023 am 11:34 AM

React responsive design guide: How to achieve adaptive front-end layout effects

See all articles