meteor - 如何使用 Meteor 和 React 一致地确定用户名
问题描述
我有一个简单的流星应用程序,其中包含一个显示用户名的组件:
const Welcome = ({ isLoading, username }) => {
if (isLoading) {
return <Loader />;
} else {
return <div>{username}</div>;
}
};
我正在尝试通过传递用户名
const WelcomeContainer = withTracker(() => {
const isLoading = !Meteor.user() || Meteor.loggingIn();
const username = Meteor.user() ? Meteor.user().username : '';
return {
isLoading,
username,
};
})(Welcome);
如果我正常导航到组件,一切似乎都正常。但是,如果我刷新页面,大约有一半的时间会卡在加载组件上。(值得注意的是,我正在使用 ReactRouter)
更重要的是日志非常相似:
console.log(`client: ${Meteor.isClient} and isLoading: ${isLoading}`);
// Username displayed successfully
client: true and isLoading: true (x2)
client: true and isLoading: true (x2)
client: true and isLoading: false (x2)
// Stuck on Loading
client: true and isLoading: true (x2)
client: true and isLoading: false (x2)
我误解了跟踪器吗?我需要在这里订阅一些东西吗?如何让组件始终显示用户名?
解决方案
问题是我有<React.StrictMode>
。删除它可以解决问题。
推荐阅读
- java - java.lang.ClassNotFoundException: com.amazonaws.transform.EnhancedJsonErrorUnmarshaller
- php - 如何在 foreach 循环中访问 PHP 中的变量?
- c - 并发非重叠 pwrite() 到使用多个 MPI 进程安装在 NFS 上的文件
- oracle - 在具有不同数据库服务器的客户端上安装 ORACLE APEX
- wordpress - 如果 url 等于 functions.php 中的某些内容,则在标题上添加元标记
- python - 我无法在 TKINTER 中播放 GIF
- arrays - array.map 不是函数,reactjs setState 错误
- vue.js - Nuxtjs中如何导出整个项目中的组件?
- python - 熊猫如何将_excel x读取到最后一列?
- java - Autowired Spring Bean 在抽象父类中为空