Is there a recommended pattern for passing props to descendant components in React?
Below, I'm passing the prop callback
to a child component:
Master = React.createClass({
render: function() {
return (
<div>
<ListComp items={this.props.items} callback={this.handleClick} />
</div>
);
}
});
ListComp = React.createClass({
render: function() {
this.props.items.forEach(function(item) {
items.push(<ItemView item={item} callback={this.props.callback} />);
}, this);
return (
<ul>{items}</ul>
);
}
});
And then the callback
prop gets handed down to the descendant component:
ItemComp = React.createClass({
render: function() {
return (
<li><a onClick={this.handleClick} href="#">Link</a></li>
);
},
handleClick: function(e) {
e.preventDefault();
this.props.callback();
}
});
Is it correct to pass the prop down twice like this or should I be referencing its inheritance somehow?
I see a transferPropsTo
method in the docs, and from logging it looks like I could get to callback
from the descendant via this.props.__owner__.props
but those double-double underscores make me think I shouldn't.