博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
React的异步组件
阅读量:7219 次
发布时间:2019-06-29

本文共 722 字,大约阅读时间需要 2 分钟。

最近在学习react,用到了异步组件,实现按需加载chunk.js,减轻首页压力。话不多说,直接上代码:

import React, { Component } from "react";export default function asyncComponent(importComponent) {  class AsyncComponent extends Component {    constructor(props) {      super(props);      this.state = {        component: null      };    }    async componentDidMount() {      const { default: component } = await importComponent();      this.setState({component});    }    render() {      const C = this.state.component;      return C ? 
: null; } } return AsyncComponent;}复制代码

在使用某个组件时:

import asyncComponent from './untils/asyncComponent';//异步组件的位置const Do = asyncComponent(() => import('./pages/Do'));复制代码

转载于:https://juejin.im/post/5c0a16eae51d4524fa678c19

你可能感兴趣的文章
iOS-数据持久化-对象归档
查看>>
iOS开发UI篇—程序启动原理和UIApplication
查看>>
MUI 里js动态添加数字输入框后,增加、减少按钮无效
查看>>
python pip 更换国内安装源(windows)
查看>>
结对编程2后篇
查看>>
oracle exp 和 imp 数据和表结构互相独立导出导入
查看>>
iphone-common-codes-ccteam源代码 CCNSPredicate.m
查看>>
这次项目中应该注意的问题和应该保持的好习惯
查看>>
python-数据结构化与保存
查看>>
LeetCode - 551. Student Attendance Record I
查看>>
Java用户线程和守护线程
查看>>
ClassLoader类加载机制&&JVM内存管理
查看>>
Caml语句 查询分配给当前用户及当前组
查看>>
记一次源码分析
查看>>
php版本引起的const问题
查看>>
js实现60s倒计时效果
查看>>
【POJ 2176】Folding
查看>>
redis的过期策略以及内存淘汰机制
查看>>
阿牛的EOF牛肉串
查看>>
随笔2013/2/13
查看>>