简体   繁体   English

es6传播算子如何在对象上进行道具变换?

[英]How does es6 spread operator work on an object to props transformation?

I know in order for the {...todo} to work in the Todo component, it has to be transformed to props in something like: completed=false text="clean room" but how does the spread operator do this? 我知道为了使{...todo}在Todo组件中工作,必须将其转换为props,例如: completed=false text="clean room"但是散布算子如何做到这一点? Wouldn't the current {...todo} be transformed to {completed:false}, {text:"clean room"} ? 当前的{...todo}不会转换为{completed:false}, {text:"clean room"}吗?

const TodoList = ({ todos, onTodoClick }) => (
  <ul>
    {todos.map(todo =>
      <Todo
        key={todo.id}
        {...todo}
        onClick={() => onTodoClick(todo.id)}
      />
    )}
  </ul>
)

The es6 spread operator indeed works by transforming {...todo} to {completed:false} , {text:"clean room"} . es6 spread运算符确实可以通过将{...todo} to {completed:false}转换{...todo} to {completed:false}{text:"clean room"}

However, the same operator is used in JSX but it's not necessarily the same. 但是,JSX中使用了相同的运算符,但不一定相同。

From msdn docs , msdn docs

The spread syntax allows an expression to be expanded in places where multiple arguments (for function calls) or multiple elements (for array literals) or multiple variables (for destructuring assignment) are expected. 扩展语法允许在需要多个参数(用于函数调用)或多个元素(用于数组文字)或多个变量(用于解构赋值)的地方扩展表达式。

Following this idea, the JSX spread operator was created. 按照这个想法,创建了JSX spread operator According to the React docs , 根据React文档

The ... operator (or spread operator) is already supported for arrays in ES6. ES6中的数组已经支持...运算符(或散布运算符)。 There is also an ECMAScript proposal for Object Rest and Spread Properties. 还有一个ECMAScript建议书,涉及对象剩余和传播属性。 We're taking advantage of these supported and developing standards in order to provide a cleaner syntax in JSX. 我们利用这些受支持和正在开发的标准,以便在JSX中提供更简洁的语法。

Since this already follows the idea of the spread operator, a unification of the two is indeed a welcome idea by the community 因为这已经遵循了传播运营商的想法,所以两者的统一确实是社区欢迎的想法。

As already established, the use of the spread syntax here has nothing to do with ES6, its a JSX construct. 正如已经确定的,此处使用传播语法与ES6(它是一个JSX构造)无关。

JSX is just syntactic sugar for a React.createElement call. JSX只是React.createElement调用的语法糖。 The props are actually passed as an object to that function. 道具实际上是作为对象传递给该函数的。

You can use the Babel repl to have a look how the JSX is transformed. 您可以使用Babel repl来了解JSX的转换方式。

Without spread prop: 没有传播道具:

// In
<Component foo="bar" baz="42" />

// Out
React.createElement(
  Component,
  { foo: "bar", baz: "42" }
);

With spread prop: 使用传播道具:

// In
<Component foo="bar" {...xyz} baz="42" />

// Out
React.createElement(
  Component,
  Object.assign({ foo: "bar" }, xyz, { baz: "42" })
);

So you can see, if the props contain a spread prop, they are separated into multiple objects and simply merged. 因此,您可以看到,如果这些道具包含一个展开的道具,它们将被分成多个对象并被简单地合并。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM