2015-06-05 67 views
0

我是新来反应Js,并试图了解如何通过Ajax(如此处所述:Reactjs - loadResourcesFromServer - combine two sources)从不同来源获得的数据上进行合成。反应js作文

我有一个Comment对象和Author对象。

var Author = React.createClass({ 
    ... 
    render: function() { 
     return (
      <div><a>{this.state.data.username}</a><img src="{this.state.data.picture}" /></div> 
     ); 
    } 
}); 

var Comment = React.createClass({ 
    ... 
    render: function() { 
     return (
      <div><p>{this.state.title}</p></div> 
     ); 
    } 
}); 

调用<作者/>和<评论/>回2个独立的div,但我想显示类似

<div> 
    <p> 
     <img src="{this.state.data.picture}" /> 
     {this.state.title}, published by {this.state.data.username} 
    </p> 
</div> 

我试图用一个包装

<CommentWithAuthor> 
    <Author/> 
    <Comment/> 
</CommentWithAuthor> 

但只能访问这个包装器中的对象的道具,而不是对象本身。由于我需要一个新的渲染器(React.js - access to component methods),因此我无法使用参考。

有什么想法?谢谢!

+0

我没有得到你想要实现的。你想从服务器获取数据吗(你会收到一个包含作者和评论值的注释的对象,并且你希望相应地将这些值分配给Author和Comment?你能更具体一些吗? – rmuller

+0

Hi rmuller,I尝试将两个ajax结果的值混合到一个显示html的对象中,即

user photo, comment title, published by username, read x times
Raphael

回答

1

你应该使用一些层次,例如,包括注释块作为作家块的一部分,通过它的作者ID,如果你有一个以上的作者和基于作者

var Author = React.createClass({ 
    ... 
    render: function() { 
     return (
      <div><img src="{this.state.data.picture}" /> 
       <comment authoriid={this.props.id}/> 
       <a>{this.state.data.username}</a> 
      </div> 
     ); 
    } 
}); 

选择以下评论你正在将数据从顶层(作者)发送到下一级作为预期的设计在reactjs

+0

好的,谢谢arkoak,层次结构+1。如果我有多个注释要素返回,请参阅

comment title, published by username, read x times
?谢谢。 ! – Raphael

+0

请检查:http://stackoverflow.com/a/28320488/724913,jsfiddle有一个数据数组用于循环和显示所有的数据,你需要和你的数据数组一样使用注释 – arkoak

+0

很好,谢谢! – Raphael