How to apply Tracker.autorun? Meteor.userId() undefined right after refreshing page

我怕爱的太早我们不能终老 提交于 2019-12-06 16:41:40

Tracker.autorun allows you a function to be called automatically whenever it's dependent reactive data source changes.

Simply speaking, Tracker.autorun() takes a function as input, runs this function now and returns whenever the data source changes later on.

In your case, you can use Tracker.autorun() for tracking the user document, since Meteor.user() and Meteor.userId() are reactive. In componentDidMount() call Tracker.autorun() and save the user document in elsewhere when it changes.

Hope following code snippet helps:

componentDidMount() {
        var context = this;

        Tracker.autorun(function(){
            let user = Meteor.user();
            if (user != undefined) {
                context.setState({ user: user, });
            }
        });
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!