Reactjs 反应什么';这是从孩子那里得到父母道具的正确方法
耐心点,我是新手 在这个片段中(它不起作用),我想要 在CommentForm中,获取url道具的值 从评论框中Reactjs 反应什么';这是从孩子那里得到父母道具的正确方法,reactjs,Reactjs,耐心点,我是新手 在这个片段中(它不起作用),我想要 在CommentForm中,获取url道具的值 从评论框中 var CommentBox = React.createClass({ render: function() { return ( <div className="commentBox"> <h1>Comments</h1> <CommentList /> <
var CommentBox = React.createClass({
render: function() {
return (
<div className="commentBox">
<h1>Comments</h1>
<CommentList />
<CommentForm />
</div>
);
}
});
var CommentList = React.createClass({
render: function() {
return (
<div className="commentList">
Hello, world! I am a CommentList.
</div>
);
}
});
var CommentForm = React.createClass({
render: function() {
return (
<div className="commentForm">
Hello, my test url {this.props.url} !.
</div>
);
}
});
React.render(
<CommentBox url="api/comments" />,
document.getElementById('content')
);
var CommentBox=React.createClass({
render:function(){
返回(
评论
);
}
});
var CommentList=React.createClass({
render:function(){
返回(
你好,世界!我是一个评论列表。
);
}
});
var CommentForm=React.createClass({
render:function(){
返回(
你好,我的测试url{this.props.url}!。
);
}
});
反应(
,
document.getElementById('content')
);
正确的方法是什么
为什么道具不能直接从父代传递给子代?您需要明确地将道具从父代传递给子代。更改
CommentBox
的render
功能将解决以下问题:
var CommentBox = React.createClass({
render: function() {
return (
<div className="commentBox">
<h1>Comments</h1>
<CommentList />
//The next line is where you can pass the URL to the CommentForm
<CommentForm url={this.props.url}/>
</div>
);
}
});
var CommentBox=React.createClass({
render:function(){
返回(
评论
//下一行是您可以将URL传递到CommentForm的地方
);
}
});
根据您的示例改编的JSFIDLE:
文件上说“对于亲子沟通,只需传递道具即可。”参见
作为显式传递道具的替代方案,React的undocumentedcontext
功能更接近您想要的:
并且在1.0的路线图上
也就是说,传递道具是正常的模式