什么是;..这个道具;ReactJS中的平均值

What does "...this.props" mean in ReactJS?

本文关键字:ReactJS 平均值 什么      更新时间:2024-05-22

{...this.props}在这段代码中是什么意思?

<div {...this.props} style={{ height: `100%`, }}

{...variable}语法称为"排列属性"。

它所做的基本上是,它获取this.props(或任何其他传递的变量)的每个属性,并将它们应用于元素。

示例:

props = {className: 'big', href: 'http://example.com'};
<a {...props} />
// the above line is equal to the following
<a className="big" href="http://example.com" />

我认为可能是排列运算符(三个点)让您偏离了?:)

反应中的三个点做什么?

编辑:要详细说明,您可能正在查看一个JSX模板?事实上,每个属性都将是结果HTML中样式属性的CSS属性。此外,spread运算符使this.props中的所有属性都得到扩展,即与this.props

{…this.props}表示当前组件的所有props。假设你在props中有对象a和对象b,而{…this.props}的意思是a和b。你可以使用它将当前组件的所有prop传递给另一个组件。