[英]Meteor - Display the Email Address of the Logged In User
我正在嘗試顯示使用Meteor登錄的用戶的電子郵件地址。
我正在使用Meteor.user()。emails [0] .address命令-有時僅能使用。 其他時候它是不確定的。 這是因為有時頁面在用戶的收藏集可用之前呈現。
但是,我正在使用React而不是大火。 每個在線解決方案都建議在模板的onCreated部分中使用Meteor.subscribe()。 但是我無法弄清楚React的等效對象,也無法弄清楚如何在渲染之前等待User集合。
已更新為使用Meteor.autorun,它接受在Meteor的反應性源更新時運行的回調函數。
Meteor.subscribe接受onReady
可選回調。 我會在React組件上附加componentWillMount
生命周期事件,設置流星訂閱,並在onReady
觸發后導致狀態更改。 這是一些粗略的示例代碼;
var Foo = React.createClass({
componentWillMount: function() {
var _this = this;
// Setup meteor subscription
Meteor.autorun(function () {
_this.setState({
user: Meteor.user(),
});
})
},
render: function() {
// Render nothing until we have a user
if (!this.state || !this.state.user) {
return null;
}
// Render the address when we have the user
return (
<div>{this.state.user.emails[0].address}</div>
);
}
});
相關文檔: http : //docs.meteor.com/api/pubsub.html#Meteor-subscribe
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.