【发布时间】:2019-10-22 04:27:01
【问题描述】:
我刚刚使用
accounts-password 包。身份验证本身工作正常。
在我看来,有些按钮和东西应该只显示给登录的用户。我通过检查Meteor.userId() 得到了这个工作。如果null,按钮将不会显示。
问题如下: 当我登录(或退出)时,我的视图不会重新呈现。它根本不隐藏或显示应切换的按钮。总是需要一个独立的重新渲染来显示或隐藏它们。
Meteor-Documentation 声明,Meteor.userId() 应该是响应式的,但它在我的组件中的行为方式并非如此。
这里有一些 react-component 显示我的视图如何根据Meteor.userId()显示或隐藏按钮:
class SomeReactComponent extends React.Component {
constructor(props) {
super(props);
...
}
...
render() {
return (
<span>
<p>Text that should be displayed to anyone!</p>
{ Meteor.userId() ? (
<button id="btn">
This button is only available to logged in users
</button>
) : ""}
</span>
);
}
}
【问题讨论】: