我正在尝试实现React.js网站上给出的注释教程,但是具有一些额外的功能。我想通过注释对象传递相关的用户数据,如用户名和头像。我了解如何将道具从父母传递到孩子的组件,但是我们如何从父母->孩子->孩子传递数据?或父-> subChild?
父CommentBox组件:
<CommentList comments= {this.state.comments} avatar= {this. state. avatar} count={this.state.comments_count} user= {this.state.user} />
评论列表组件:(评论列表返回评论组件)
var CommentList = React.createClass({
render: function () {
var commentNodes = this.props.comments.map(function (comment) {
<Comment comment={ comment.comment } key={ comment.id } avatar = "how to pass the parent avatar state" count = "how to pass parent count state" />
});
});
如何将道具访问Comment组件,该组件负责呈现单个评论?请注意,Comment组件是CommentBox Component的SubChild。
谢谢
最佳答案
它只是JavaScript,因此您可以将其存储在变量中。 var self = this
,var props = this.props
,var {count,avatar} = this.props
(es6)等有多种方法。
var CommentList = React.createClass({
render: function () {
var count = this.props.count;
var avatar = this.props.avatar
var commentNodes = this.props.comments.map(function (comment) {
<Comment comment={ comment.comment } key={ comment.id }
avatar={ avatar } count={ count } />
});
});
关于javascript - 将Pass关联数据(prop)传递给后代,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25421635/