【问题标题】:Meteor react render subscribe流星反应渲染订阅
【发布时间】:2018-01-07 21:10:22
【问题描述】:

如何使 PostList 类组件从订阅中呈现,我真的没有获得 withtracker 功能,我用谷歌搜索并找到了不同的版本,但我无法弄清楚它是否可以工作,我做错了什么以及我应该怎么做是吗?我确实重新阅读了文档,但没有太大帮助,请解释一下。

react-meteor-data 包

this is my mongo db.posts.find()
{ " _id " : ".." , " data " :{ " title " : " s " , " description " : "a" }, " userid " : "..." , " createdat ":131231}

my publish Meteor.publish("postlist",function(){
return Posts.find({});
})

my react component 
export default class PostList extends React.Component{
    render(){
        return{

        }
    }
}

withTracker((props) => {
    const handle = Meteor.subscribe('postlist');

    return {
        loading: !handle.ready(),
        posts: Posts.find().fetch()
    }
}, PostList)

【问题讨论】:

    标签: reactjs meteor


    【解决方案1】:

    您需要从withTracker 导出新创建的组件,而不是PostList 本身。

    withTracker 也是一个 HOC,它返回一个你需要组件包装的函数。

    class PostList extends React.Component {
        render () {
            return {
                <div>Some content</div>
            }
        }
    }
    
    export default withTracker((props) => {
        const handle = Meteor.subscribe('postlist');
    
        return {
            loading: !handle.ready(),
            posts: Posts.find().fetch()
        }
    })(PostList)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-21
      • 1970-01-01
      • 2017-01-04
      • 2013-12-24
      • 2016-06-26
      • 1970-01-01
      • 2016-02-17
      • 2012-10-01
      相关资源
      最近更新 更多