javascript - 使用 componentDidMount() 在刷新时停止页面闪烁
问题描述
我一直在添加一个会话来保持登录,当然我的应用程序中的每个用户都有唯一的会话。当我登录并刷新页面时,登录模式会闪烁,因为登录状态为假,直到会话检查完成。我一直在阅读它,似乎可以通过加载占位符并在 componentDidMount 生命周期中使用 async/await 或 promise 来解决这个问题。但是它不起作用。我尝试了几种方法,现在我尝试了以下方法,但我有点迷失,因为我尝试过的各种方法都没有奏效。
export default class BasePage extends Component {
constructor() {
super()
this.state = {
loggedIn: false,
userEmail: null,
userPassword: null,
userName: null,
registerUserForm: false,
sessionActive: false,
sessionID: null,
fullPageLoad: null
}
this.checkSession = this.checkSession.bind(this)
}
componentDidMount() {
(async () => {
await this.checkSession()
this.setState({
fullPageLoad: true
})
})()
}
// React page body
render() {
if (this.state.fullPageLoad === null) {
return(
<div>Loading ...</div>
)
} else {
return (
// Render page content
)
}
}
解决方案
推荐阅读
- java - Java Spring 调度服务没有获取最新数据
- vba - 搜索单个或多个文本组合的 FlagRequest
- angular - 你如何使这个可观察的内部可观察的工作?
- javascript - 网络无障碍导航
- javascript - Ajax : 响应值与 php 文件中的不一样
- python - Django 模型 create() 和 save() 不持久保存在数据库中(实例上的 pk / id 属性未设置)
- reactjs - npm 启动问题:npm ERR!代码生命周期
- python - Python 代码的行为不同,具体取决于我是将列表直接传递给构造函数还是作为变量传递
- ios - Xcode:如何知道呈现当前视图的viewController?
- django - 仅当您通过身份验证且仅使用 django-rest-framework 向您的用户发布数据