如何应用Tracker.autorun?刷新页面后立即定义Meteor.userId() [英] How to apply Tracker.autorun? Meteor.userId() undefined right after refreshing page

查看:114
本文介绍了如何应用Tracker.autorun?刷新页面后立即定义Meteor.userId()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Meteor.userId()和角色有一个受限制的页面:

I have a restricted page using Meteor.userId() and roles:

class AdminPage extends Component {
render() {
 return (
  <div>
    {
      Roles.userIsInRole(Meteor.userId(), 'admin') ? (
        <Master_Layout renderCenter={<Article_Editor />}/>
      ) : browserHistory.push('/')
    }
  </div>
)
}
}

由于未定义Meteor.userId(),因此此代码在刷新后将用户重定向到"/".在刷新后呈现页面之前,如何确保Meteor.userId()没有未定义?

This code redirects user to "/" after refresh because Meteor.userId() is undefined. How do I make sure Meteor.userId() is not undefined before rendering the page after refresh?

我搜索了答案.我找到Tracker.autorun作为解决方案,但我不知道如何应用它.

I searched for answers. I found Tracker.autorun as a solution, but I did not understand how to apply it.

感谢您的帮助.我更新了代码:

Thank you for help. I updated the code:

constructor(props) {
super(props);

this.state = { user: '' };
}
componentDidMount() {
var $this = this;

Tracker.autorun(function () {
  let user = Meteor.user();
  if(user != undefined) {
    $this.setState({ user: user });
  }
});
}
render() {
if(Roles.userIsInRole(this.state.user, 'admin')) {
  return (
    <div>
      <Master_Layout renderCenter={<Article_Editor />} />
    </div>
  )
} else {
  return <div>loading...</div>
}
}
}

需要的零件:
$ this = this;
user = Meteor.user();//Meteor.userId()不起作用.
删除了browserHistory,因为它将在定义用户之前导航离开.

现在,我只需要找到一种在渲染挂载之前定义user/userId的解决方案.

Observed needed parts:
$this = this;
user = Meteor.user(); //Meteor.userId() does not work.
removed browserHistory because it will navigate away before user is defined.

Now I just need to find a solution on defining user/userId before render mounts.

推荐答案

Tracker.autorun允许您在依赖于反应式数据源的任何变化时自动调用该函数.

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

简单来说,Tracker.autorun()将一个函数作为输入,现在运行此函数,并在以后数据源发生任何更改时返回.

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

根据您的情况,您可以使用Tracker.autorun()跟踪用户文档,因为Meteor.user()Meteor.userId()是反应性的.在componentDidMount()中,调用Tracker.autorun()并将更改后的用户文档保存在其他位置.

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, });
            }
        });
    }

这篇关于如何应用Tracker.autorun?刷新页面后立即定义Meteor.userId()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆