2016-12-07 71 views
1

美好的一天。我正在尝试使用react-bootstrap库。
而在它的tutorial我看到了ES6代码,这似乎有点让我感到困惑。可以使用对象属性吗?

function FieldGroup({ id, label, help, ...props }) { 
    return (
    <FormGroup controlId={id}> 
     <ControlLabel>{label}</ControlLabel> 
     <FormControl {...props} /> 
     {help && <HelpBlock>{help}</HelpBlock>} 
    </FormGroup> 
); 
} 

是否可以接受使用单独的对象的属性,而不引用到一个对象?

+1

这是*对象解构语法... * https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment这有帮助吗? – deceze

回答

2

这很好。它被称为destructuring。 它比这更清洁:

function FieldGroup(props) { 
    return (
    <FormGroup controlId={props.id}> 
     <ControlLabel>{props.label}</ControlLabel> 
     <FormControl {...props} /> 
     {help && <HelpBlock>{props.help}</HelpBlock>} 
    </FormGroup> 
); 
} 

它还具有not just sending all properties的优势<FormControl />,但只需要的人。

相关问题