众所周知,在项目中如果在资源加载请求还未完成的时候,由于阻塞机制,会出现首页白屏的问题,产生很差的用户体验。本文以react为例,提供一个解决方法。

解决原理:使用 onreadystatechange 去监听 readyState,在资源加载完成之前加载一个只有框架的静态页面,页面不请求数据。当数据请求完成之后再将路由切换到真实的首页。

废话不多说,上代码:

main.js

import React from 'react'
import ReactDom from 'react-dom'
import {Provider} from 'react-redux'
import {BrowserRouter as Router, Route} from 'react-router-dom'
import configureStore from './store'
import Index from './containers/Index.js'
import FirstScreen from './containers/FirstScreen.js' export const store = configureStore() function listen () {
if (document.readyState == 'complete') { // 资源加载完成
ReactDom.render(
<Provider store={store}>
<Router>
<Route path="/" component={Index}/>
</Router>
</Provider>,
document.getElementById('root')
)
} else { // 资源加载中
ReactDom.render(
<Provider store={store}>
<Router>
<Route path="/" component={FirstScreen}/>
</Router>
</Provider>,
document.getElementById('root')
)
}
} document.onreadystatechange = listen

其中Index.js就是你的真实首页,FirstScreen.js就是只有框架的静态页。

Index.js
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import {connect} from 'react-redux'
import {store} from '../main'
import {bindActionCreators} from 'redux'
import {getLocalTime} from '../actions/localTime'
import LocalTime from '../components/LocalTime'
import '../static/css/Index.css' class Index extends Component {
/**
* constructor() React组件的构造函数在挂载之前被调用。
* 在实现React.Component子类的构造函数时,
* 应该super(props)在任何其他语句之前调用。
* 否则,this.props会在构造函数中定义,这可能会导致错误。
*/
constructor (props) {
super(props)
this.realTime = this.realTime.bind(this)
} realTime () {
setInterval(() => {
store.dispatch(getLocalTime())
}, 1000)
} /**
* componentWillMount()会在组件render之前立即被调用,并且永远都只执行一次。
* 由于这个方法始终只执行一次,所以如果在这里定义了setState方法之后,页面永远都只会在加载前更新一次。
*/
componentWillMount () {
} /**
* componentDidMount()在组件被装载后立即被调用。
* 在这个时候之后组件已经生成了对应的DOM结构。
* 可以在这个方法中执行setTimeout, setInterval,接口调用等。
*/
componentDidMount () {
this.realTime()
} /**
* componentWillReceiveProps()在组件接收到一个新的prop时被执行。
* 这个方法在初始化render时不会被调用。
*/
componentWillReceiveProps () {
} /**
* 返回一个布尔值。在组件接收到新的props或者state时被执行。
* 在初始化时或者使用forceUpdate时不被执行。
* 如果shouldComponentUpdate返回false,
* render()则会在下一个state change之前被完全跳过,componentWillUpdate和 componentDidUpdate也不会被执行
*/
shouldComponentUpdate (nextProps, nextState) {
return true
} /**
* componentWillUpdate()在组件接收到新的props或者state但还没有render时被执行。
* 在初始化时不会被执行。
*/
componentWillUpdate (nextProps, nextState) {
} /**
* componentDidUpdate()在组件完成更新后立即执行。
* 在初始化时不会被执行。一般会在组件完成更新后被使用。
* 可以用来 clearInterval。
*/
componentDidUpdate (prevProps, prevState) {
clearInterval(this.realTime())
} /**
* render()函数应该是纯粹的,这意味着它不会修改组件状态,
* 每次调用时都会返回相同的结果,并且不会直接与浏览器交互
*/
render () {
const {localTime} = this.props
return (
<div className='main'>
<LocalTime localTime={localTime} getLocalTime={getLocalTime}></LocalTime>
</div>
)
}
} Index.propTypes = {
localTime: PropTypes.string,
getLocalTime: PropTypes.func
} // 将state绑定到props
const mapStateToProps = (state, ownProps) => {
const {localTime} = state
return {
localTime: localTime.localTime
}
} // 将action绑定到props上
const mapDispatchToProps = (dispatch, ownProps) => {
return {
getLocalTime: bindActionCreators(getLocalTime, dispatch)
}
} // 通过react-redux提供的connect方法将我们需要的state中的数据和actions中的方法绑定到props上
export default connect(mapStateToProps, mapDispatchToProps)(Index)
FirstScreen.js
import React, {Component} from 'react'
import {connect} from 'react-redux'
import '../static/css/FirstScreen.css' class FirstScreen extends Component {
constructor (props) {
super(props)
} render () {
return (
<div className='firstScreen'>
我是首屏空白页
</div>
)
}
} export default connect()(FirstScreen)

本文转载自:https://www.cnblogs.com/Man-Dream-Necessary/p/7994130.html

解决React首屏加载白屏的问题的更多相关文章

  1. React Native 首次加载白屏优化

    RN首次加载都会有个白屏过程,一般都会有500ms+的白屏时间,原生页面开发同样的页面会能够快速显示而在RN页面中有个明显的等待过程,这个会影响用户体验. 1.使用过渡页面 简单处理可以在白屏过程中加 ...

  2. iOS 解决LaunchScreen中图片加载黑屏问题

    iOS 解决LaunchScreen中图片加载黑屏问题 原文: http://blog.csdn.net/chengkaizone/article/details/50478045 iOS 解决Lau ...

  3. vue首次加载白屏过渡动画(vue优化)

    过渡动画需要在index.html文件里面添加 1.css,在public.index.css创建index.css html, body, #app { height: 100%; margin: ...

  4. 解决React通过ajax加载数据更新页面不加判断会报错的问题

    通过AJAX加载数据是一个很普遍的场景.在React组件中如何通过AJAX请求来加载数据呢?首先,AJAX请求的源URL应该通过props传入:其次,最好在componentDidMount函数中加载 ...

  5. react native中的欢迎页(解决首加载白屏)

    参照网页: http://blog.csdn.net/fengyuzhengfan/article/details/52712829 首先是在原生中写一些方法,然后通过react native中js去 ...

  6. vue+elementui 开发的网站IE浏览器加载白屏(不兼容)解决办法

    1.需要检查一下 export default { name: 'aa',-------vue的name是不可以重复的-----这个是决定性原因 data() { return {} } 2.变量声明 ...

  7. vue实现首屏加载等待动画 避免首次加载白屏尴尬

    0 直接上效果图 1背景,用户体验良好一直是个重要的问题. 2怎么加到自己项目里面? 复制css html代码到自己的index.html即可 代码链接 源码地址 Vue学习前端群493671066, ...

  8. react 首屏加载优化

    react 首屏加载优化,原本是在入口HTML文件中加载loading动画,但是部署在测试环境上的时候一直无法显示loading的部分,也是奇怪了,我们测试环境的部署一直跟本地的都不太一样,内外网的转 ...

  9. vuejs学习之 项目打包之后的首屏加载优化

    vuejs学习之 项目打包之后的首屏加载优化 一:使用CDN资源 我们在打包时,会将package.json里,dependencies对象里插件打包起来,我们可以将其中的一些使用cdn的方式加载,例 ...

随机推荐

  1. 我就骂你了,我tm还想打你呢

    从地铁出来,一男的抽烟走在我前面,走了一路闻了一路二手烟. 进门,一个园区的,我直接骂了一句:caoni妈的 这哥们瞪着我,我也瞪着他 你骂我干什么 我闻了一路子二手烟 你可以走前面啊 我不走啊 我不 ...

  2. Html5前端笔记

    获取Dpi 在 window.load中添加: (function(){ if (!window.screen.deviceXDPI){ var tmpNode = document.createEl ...

  3. C_数据结构_循环队列

    # include <stdio.h> # include <malloc.h> typedef struct Queue { int * pBase; int front; ...

  4. Individual P1: Summary

    经过5个小时成功把simple mode写差不多了..orz 也是蛮拼的. 开始毫无头绪,本能地开始从度娘搜索‘c# 单词统计’= =看了两段代码也算是见过c#的人了.差不多花了我1小时的时间. 然后 ...

  5. Liinux 学习心得

    Linux 内核学习心得 姬梦馨 原创作品 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 反汇编一个简 ...

  6. LINUX内核分析第七周学习总结

    LINUX内核分析第七周学习总结 标签(空格分隔): 20135328陈都 陈都 原创作品转载请注明出处 <Linux内核分析>MOOC课程 http://mooc.study.163.c ...

  7. 第三个Sprint冲刺第5天

    成员:罗凯旋.罗林杰.吴伟锋.黎文衷 各成员努力完成最后冲刺

  8. 第三个spring冲刺第8天

    今天,我们忙于完成精美的背景,还有难度的具体设置,如何达到最理想化,为此我们今天主要是做了开会讨论,但还没有完全确定好结论,明天就应该能做出结论,然后修改后台的难度设置了.

  9. Spring Boot features - Profiles

    https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html https://w ...

  10. What is the best Java email address validation method?

    https://stackoverflow.com/questions/624581/what-is-the-best-java-email-address-validation-method htt ...