One approach to building high performance applications with webpack is to take advantage of code-splitting to only load the needed JavaScript on initial load and asynchronously load additional JavaScript bundles when needed. In this lesson, we'll create add some code-splitting to our placeholder application and configure our project to support the dynamic import() syntax.

Install:

npm i -D @babel/plugin-syntax-dynamic-import

Add plugin into webpack config:

plugins: [
...
'@babel/plugin-syntax-dynamic-import'
]

What we want to do is lazy loading a Warning component, and also utilize the code spliting from webpack:

Here is the component we want to lazy load in:

import React from 'react'

export default () => <span className={'warning'}>Take it easy!</span>

App.js:

import React from 'react'
import {hot} from 'react-hot-loader' const Warning = React.lazy(() => import('./Warning')) class App extends React.Component {
state = {
count: 0
} increment = () => {
this.setState(state => ({count: state.count + 1}))
} decrement = () => {
this.setState(state => ({count: state.count - 1}))
} render() {
const {count} = this.state
return (
<div>
<h1>Hello World.</h1>
<h2 className={count > 10 ? 'warning' : null}>
Count: {count}
</h2>
<button onClick={this.increment}>+</button>
<button onClick={this.decrement}>-</button>
{count > 10 ?
<React.Suspense fallback={null}>
<Warning />
</React.Suspense>

: null}
</div>
)
}
} export default hot(module)(App)

We use React.lazy + dynamic import syntax:

const Warning = React.lazy(() => import('./Warning'))

Then we use lazy loaded Warning component with React.Suspense:

<React.Suspense fallback={null}>
<Warning />
</React.Suspense>

'fallback' take a jsx element which will be shown when doing the lazy loading.

So what if the Warning component failed to load?

Here is where Error Boundries comes in to play:

class MyErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false };
} static getDerivedStateFromError(error) {
// Update state so the next render will show the fallback UI.
return { hasError: true };
} componentDidCatch(error, info) {
// You can also log the error to an error reporting service
logErrorToMyService(error, info);
} render() {
if (this.state.hasError) {
// You can render any custom fallback UI
return <h1>Something went wrong.</h1>;
} return this.props.children;
}
} export defualt MyErrorBoundry;

Then wrap your Suspense component with boundry:

<MyErrorBoundary>
<React.Suspense fallback={null}>
<Warning />
</React.Suspense>
</MyErrorBoundary>

Now we can get benifits from lazy loading and also safety from the boundry

More about Code splitting.

[React] Asynchronously Load webpack Bundles through Code-splitting and React Suspense的更多相关文章

  1. webpack优化之code splitting

    作为当前风头正盛的打包工具,webpack风靡前端界.确实作为引领了一个时代的打包工具,很多方面都带来了颠覆性的改进,让我们更加的感受到自动化的快感.不过最为大家诟病的一点就是用起来太难了. 要想愉快 ...

  2. webpack async load modules & dynamic code splitting

    webpack async load modules & dynamic code splitting webpack 按需/异步加载/Code Splitting webpack loade ...

  3. webpack Code Splitting浅析

    Code Splitting是webpack的一个重要特性,他允许你将代码打包生成多个bundle.对多页应用来说,它是必须的,因为必须要配置多个入口生成多个bundle:对于单页应用来说,如果只打包 ...

  4. [转] react-router4 + webpack Code Splitting

    项目升级为react-router4后,就尝试着根据官方文档进行代码分割.https://reacttraining.com/react-router/web/guides/code-splittin ...

  5. react-router4 + webpack Code Splitting

    项目升级为react-router4后,就尝试着根据官方文档进行代码分割.https://reacttraining.com/react-router/web/guides/code-splittin ...

  6. [Webpack 2] Maintain sane file sizes with webpack code splitting

    As a Single Page Application grows in size, the size of the payload can become a real problem for pe ...

  7. webpack 利用Code Splitting 分批打包、按需下载

    webpack中的解决方案——Code Splitting,简单来说就是按需加载(下载),如果是requireJS对应的AMD的方案中这本是在正常不过了.但是在webpack中All in one的思 ...

  8. webpack 和 code splitting

    Code Splitting指的是代码分割,那么什么是代码分割,webpack和code splitting又有什么样的联系呢? 使用npm run dev:"webpack-dev-ser ...

  9. 借助Code Splitting 提升单页面应用性能

    近日的工作集中于一个单页面应用(Single-page application),在项目中尝试了闻名已久的Code splitting,收获极大,特此分享. Why we need code spli ...

随机推荐

  1. STM32 F4 SPI Accelerometer

    STM32 F4 SPI Accelerometer

  2. [Winform]setupfactory制作安装包卸载输入密码进行验证

    摘要 项目有这样一个需求,在体验机上安装了一个软件,如果有用户卸载的时候,给与输入密码验证的提示,当然强制删除软件所在目录除外.那么这个有办法实现吗? 解决办法 在卸载的时候,用户单击下一步的时候进行 ...

  3. 基于设备树的TQ2440触摸屏驱动移植

    平台 开发板:tq2440 内核:Linux-4.9 u-boot:u-boot-2015.04   概述 之前移植了LCD驱动,下面继续移植触摸屏驱动,然后将tslib也移植上去. 正文 一.移植触 ...

  4. ASP.NET Web API实践系列07,获取数据, 使用Ninject实现依赖倒置,使用Knockout实现页面元素和视图模型的双向绑定

    本篇接着上一篇"ASP.NET Web API实践系列06, 在ASP.NET MVC 4 基础上增加使用ASP.NET WEB API",尝试获取数据. 在Models文件夹下创 ...

  5. 清除数据库表、外键、存储过程SQL

    1.删除所有外键 )    begin         exec(@c1)        fetchnextfrom c1 into@c1    endclose c1deallocate c1 2. ...

  6. JAVA BigDecimal的相加

    之前很少使用这样的一个对象BigDecimal,今天在改需求的时候遇到了,结果坑爹的怎么相加最后都为零. 代码如下: BigDecimal totalAmount = new BigDecimal(0 ...

  7. eclipse 安装 weblogic server

  8. 猫都能学会的Unity3D Shader入门指南(一)

    动机 自己使用Unity3D也有一段时间了,但是很多时候是流于表面,更多地是把这个引擎简单地用作脚本控制,而对更深入一些的层次几乎没有了解.虽然说Unity引擎设计的初衷就是创建简单的不需要开发者操心 ...

  9. 用jpinyin实现汉字转拼音功能

    一.简介 项目地址:https://github.com/stuxuhai/jpinyin JPinyin是一个汉字转拼音的Java开源类库,在PinYin4j的功能基础上做了一些改进. [JPiny ...

  10. [Web 前端] React Js img 图片显示默认 占位符

    cp from : https://blog.csdn.net/wyk304443164/article/details/77093339 没有考虑到兼容性,因为我们暂时只适配了webkit. 也没有 ...