首页 > 解决方案 > 使用 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
    )
  }
}

标签: javascriptreactjsasynchronousasync-await

解决方案


推荐阅读