众所周知,在项目中如果在资源加载请求还未完成的时候,由于阻塞机制,会出现首页白屏的问题,产生很差的用户体验。本文以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. inode 软/硬链接

    一.inode是什么? 理解inode,要从文件储存说起. 文件储存在硬盘上,硬盘的最小存储单位叫做"扇区"(Sector).每个扇区储存512字节(相当于0.5KB). 操作系统 ...

  2. springboot undertow替换tomcat方式

    版权声明: https://blog.csdn.net/weixin_38187317/article/details/81532560说明        undertow,jetty和tomcat可 ...

  3. Stanford Word Segmenter的特定领域训练

    有没有人自己训练过Stanford Word Segmenter分词器,因为我想做特定领域的分词,但在使用Stanford Word Segmenter分词的时候发现对于我想做的领域的一些词分词效果并 ...

  4. Daily Scrumming* 2015.12.21(Day 13)

    一.团队scrum meeting照片 大部分成员编译请假,故今天没有开scrum meeting 二.成员工作总结 姓名 任务ID 迁入记录 江昊 无 无 任务说明: 今日准备编译测验,请假 遇到问 ...

  5. java计算器项目

    简单的java计算器项目   题目:java计算器项目 一. 题目简介: 一个能进行加减乘除四则运算的小程序 Github链接:https://github.com/lizhenbin/test/tr ...

  6. <构建之法>10,11,12章的读后感

    第十章:典型用户和场景 问题 :什么是典型用户? 第十一章:软件设计与实现 问题 :开发人员的标准工作流程就是不断的发现BUg,修改bug来完善功能,在此过程中要等待同伴复审,在这阶段中,开发者应该如 ...

  7. ABP框架用Dapper实现通过SQL访问数据库

    ABP的框架(2) - 访问数据库   为了防止不提供原网址的转载,特在这里加上原文链接:http://www.cnblogs.com/skabyy/p/7517397.html 本篇我们实现数据库的 ...

  8. mysql数据库忘记密码时如何修改

    工具/原料 mysql数据库 cmd命令行 打开mysql.exe和mysqld.exe所在的文件夹,复制路径地址 打开cmd命令提示符,进入上一步mysql.exe所在的文件夹

  9. PAT 1013 数素数

    https://pintia.cn/problem-sets/994805260223102976/problems/994805309963354112 令P~i~表示第i个素数.现任给两个正整数M ...

  10. Failed while installing JAX-RS (REST Web Services) 1.1. org.osgi.service.prefs.BackingStoreException: Resource '/.settings' does not exist.

    https://issues.jboss.org/browse/JBIDE-10039?_sscc=t https://stackoverflow.com/questions/16836832/fai ...