简体   繁体   English

传递道具的最佳方式

[英]Best way to pass props for react component

In React Js we can pass component props in two ways: 在React Js中,我们可以通过两种方式传递组件属性:

<Component
  prop1={value}
  prop2="value"
  prop3={this.othervalue}
/>

or 要么

let props = {
  prop1: value,
  prop2: "value",
  prop3: this.othervalue
}

<Component {...props} />

What variant is better? 哪种变体更好?

Which you prefer? 您更喜欢哪个? Neither is "right", though I think you're more likely to see the first variant, since it seems to be a bit clearer to pass the props explicitly. 都不是“正确的”,尽管我认为您更可能看到第一个变体,因为显式地传递道具似乎更加清晰。 The spread params variant is generally used to avoid duplication - if you specify the props on a parent then want to pass them to the child, spelling them out explicitly on the child feels a bit laborious. 通常使用spread params变体来避免重复-如果您在父母身上指定道具,然后要将它们传递给孩子,则在孩子身上明确地拼写出来会很费力。

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

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