在个人资料页面上,我想禁止未登录的用户访问该页面。但是,Meteor.user() 对象在初始页面加载时未定义。应该如何等待 Meteor.user() 对象被定义。什么时候确定用户没有登录并且Meteor.user()被加载?
这是我的 Profile 渲染方法的一部分以及 WithTracker 方法。正如您所看到的,当 Meteor.user() 不可用时,会渲染一个 Redirect 对象。
render() {
const {user} = this.props
console.log(user) //First undefined, later defined. But already renders redirect, so it's not useful
return (
<Container fluid className='p-3 mb-5'>
{/* Not logged in */}
{!user && <Redirect
to={{
pathname: '/SignUp',
state: {
location: '/Profile'
}
}}/>}
{/* Logged in */}
...
</Container>
)
}
export default withTracker(() => {
return {
user: Meteor.user()
}
})(Profile)
最佳答案
return (
{ undefined === user ? <Container fluid className='p-3 mb-5'>
{/* Not logged in */}
{!user && <Redirect
to={{
pathname: '/SignUp',
state: {
location: '/Profile'
}
}}/>}
{/* Logged in */}
...
</Container> : 'loading'}
)
您可以尝试使用?
和:
当用户 undefined
时运行容器内的内容并在 defined
时显示加载指示器.
关于javascript - meteor react : User Object undefined at initial page load,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50010811/