[英]What is the correct way to pass a complex object to a child component using props in React when writing in ES6?
Now that we have ES6 and can use spread I've seen some people write code like this: 现在我们有了ES6并可以使用传播,我已经看到有些人编写了这样的代码:
class Parent extends Component {
...
render() {
return (
<UserProfile {...this.props.user} onClick={this.doSomething} />
)
}
}
This would set each property of user model as a prop on the UserProfile component. 这会将用户模型的每个属性设置为对UserProfile组件的支持。 The alternative would be something like this: 另一种可能是这样的:
<UserProfile user={this.props.user} onClick={this.doSomething} />
Is one of these approaches considered the better practice? 这些方法之一被认为是更好的做法吗? I know which version I'm leaning towards, but I want to know if the community has already landed on doing it one way or the other. 我知道我要使用哪个版本,但是我想知道社区是否已经以一种或另一种方式进行开发。
Few quick inputs - 快速输入-
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.