2016-08-12 75 views
1

我正在开发一个应用程序,用户可以在其中发表评论,其他人可以回复他们的评论(回复)。流星反应传递变量到儿童容器

我的问题是,当我将commentId传递给ReplyDetail组件(孩子)时,我无法访问commentId作为createContainer中的道具 - 我只能在组件中访问它。我正在通过评论循环(地图)。

这是我想过去从commentContent.jsx变量(map函数内)

<div className="section"> 
 
    <ReplyDetail commentId={comment._id} 
 
</div>

这是子组件replyDetail.jsx

import { Meteor } from 'meteor/meteor'; 
 
import React, { Component } from 'react'; 
 
import { createContainer } from 'meteor/react-meteor-data'; 
 

 
import { Comments } from '../../../imports/collections/groupMethods'; 
 

 
class ReplyDetail extends Component { 
 

 
\t render() { 
 

 
\t \t //this works 
 
\t \t const commentId = this.props.commentId; 
 
\t \t console.log('this.props.commentId ' + this.props.commentId); 
 

 
\t \t return(
 
\t \t  \t <div>{commentId}</div> 
 
\t  ) 
 
\t } 
 
} 
 

 
export default createContainer((props) => { 
 
\t Meteor.subscribe('comments'); 
 

 
\t //this doesn't work 
 
\t \t const commentId = this.props.commentId; 
 
\t \t console.log('this.props.commentId-2 ' + this.props.commentId); 
 
    return { 
 
    }; 
 
}, ReplyDetail)

这是我得到的错误。

警告:在React性能 测量代码中存在内部错误。没想到componentDidMount定时器启动 ,而componentWillMount定时器仍在进行另一个 实例。

我需要答复是反应性的,所以如何将commentId(道具)直接传递给容器 - 或者如何将commentId从组件传递到容器?

回答

1

是的,它不工作,因为你的createContainer功能已经接收参数(props)道具,所以你不需要this. 这应该可以解决:

import { Meteor } from 'meteor/meteor'; 
import React, { Component } from 'react'; 
import { createContainer } from 'meteor/react-meteor-data'; 
import { Comments } from '../../../imports/collections/groupMethods'; 

class ReplyDetail extends Component { 

render() { 
    const {commentId} = this.props; 
    console.log('commentId', commentId); 
    return (
     <div>{commentId}</div> 
    ) 
    } 
} 

export default createContainer((props) => { 
    Meteor.subscribe('comments'); 
    const commentId = props.commentId; 
    console.log('commentIdFromContainer', commentId); 
    return { 

    }; 
}, ReplyDetail) 
+1

辉煌 - 感谢@ilan哈萨诺夫 - 就像魅力:) –