2016-08-12 48 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哈薩諾夫 - 就像魅力:) –