简体   繁体   English

为什么可以在属性中使用{…rest}

[英]Why {…rest} can be used in attributes

I just want to know why the following original codes can be shortened using {...rest} in arguments and spread in attributes as you can see in the simplified code. 我只想知道为什么可以在参数中使用{...rest}来缩短以下原始代码,并在属性中进行扩展,就像在简化代码中看到的那样。

In the simplified code it uses {...rest} spread to make value={value} onChange={onChange} type={type} . 在简化代码中,它使用{...rest}扩展使value={value} onChange={onChange} type={type} I am not sure how it is possible. 我不确定这怎么可能。

Original code 原始码

import React from "react";

const Input = ({ type, name, label, error, value, onChange }) => {
  return (
    <div className="form-group">
      <label htmlFor={name}>{label}</label>
      <input 
        value={value}
        onChange={onChange}
        type={type}
        name={name} 
        id={name} 
        className="form-control" />
      {error && <div className="alert alert-danger">{error}</div>}
    </div>
  );
};

export default Input;

Simplified code 简化代码

import React from "react";

const Input = ({ name, label, error, ...rest }) => {
  return (
    <div className="form-group">
      <label htmlFor={name}>{label}</label>
      <input {...rest} name={name} id={name} className="form-control" />
      {error && <div className="alert alert-danger">{error}</div>}
    </div>
  );
};

export default Input;

...rest is used to put all the properties that are not destructured in a separate object. ...rest用于将所有未分解的属性放在单独的对象中。

 const obj = { name: 'name', label: 'label', error: 'error', foo: 'foo', bar: 'bar' }; const { name, label, error, ...rest } = obj; console.log(rest); 

This rest object is then used for the spread syntax to pass each property in the object as a separate prop. 然后,将该rest对象用于spread syntax以将对象中的每个属性作为单独的prop传递。 It might be easier to see why this works if you write the JSX as compiled React.createElement calls. 如果将JSX编写为已编译的React.createElement调用,可能会更容易理解为什么这行得通。

React.createElement("input", {
  name: name,
  id: name,
  className: "form-control",
  ...rest
});

 const obj = { name: 'name', label: 'label', error: 'error', foo: 'foo', bar: 'bar' }; const { name, label, error, ...rest } = obj; const result = { name: name, id: name, className: "form-control", ...rest }; console.log(result); 

In the first snippet, you are accepting type , name , label , error , value and onChange then using their values directly as you stated. 在第一个代码段中,您将接受typenamelabelerrorvalueonChange然后按照您的说明直接使用它们的值。

In the second snippet, you use the rest operator (from MDN ): 在第二个片段中,您使用rest运算符(来自MDN ):

A function's last parameter can be prefixed with ... which will cause all remaining (user supplied) arguments to be placed within a "standard" javascript array. 函数的最后一个参数可以加上...前缀,这将导致所有剩余的(用户提供的)参数都放在“标准” javascript数组中。 Only the last parameter can be a "rest parameter". 只有最后一个参数可以是“其余参数”。

The rest parameter works the same in vanilla JavaScript: rest参数在原始JavaScript中的工作原理相同:

 const data = { "item1": "apples", "item2": "bananas", "item3": "grapes", "item4": "limes" }; function f({item1, item2, ...data}) { console.log("item1:", item1); console.log("item2:", item2); console.log("item3:", data.item3); console.log("item4:", data.item4); } f(data); 

As does the spread operator (from MDN ): 与传播算子(来自MDN )一样:

Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected. 扩展语法允许将迭代式(例如数组表达式或字符串)扩展到期望零个或多个参数(对于函数调用)或元素(对于数组文字)的扩展,或将对象表达式扩展在零个或多个键值对(用于对象文字)是必需的。

 const data = { "item1": "apples", "item2": "bananas", "item3": "grapes", "item4": "limes" }; const f = (items) => { for (let x in items) { console.log(x + ":", items[x]); } } f({...data}) 

The fact that you are using them with React components makes no difference. 您将它们与React组件一起使用的事实没有任何区别。

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

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