[React] Lazy loading module

To understand lazy loading in React, we need to think in two steps.

 

1. Use dynamice import: to load script

2. Use React.lazy to load dynammice import, it will hook up with a component

const loadGlobe = () => import('../globe')
const Globe = React.lazy(loadGlobe)

 

Define like this is good because we can do prefetching by dynamice import. And only when the component by using React.lazy.

复制代码
// Code splitting
// http://localhost:3000/isolated/exercise/01.js

import React, {useEffect} from 'react'
// 🐨 use React.lazy to create a Globe component which using a dynamic import
// to get the Globe component from the '../globe' module.

const loadGlobe = () => import('../globe')
const Globe = React.lazy(loadGlobe)

function App() {
  const [showGlobe, setShowGlobe] = React.useState(false)

  // 🐨 wrap the code below in a <React.Suspense /> component
  // with a fallback.
  // 💰 try putting it in a few different places and observe how that
  // impacts the user experience.
  return (
    <div
      style={{
        display: 'flex',
        alignItems: 'center',
        flexDirection: 'column',
        justifyContent: 'center',
        height: '100%',
        padding: '2rem',
      }}
    >
      <label style={{marginBottom: '1rem'}}>
        <input
          type="checkbox"
          checked={showGlobe}
          onMouseEnter={e => loadGlobe()}
          onChange={e => setShowGlobe(e.target.checked)}
        />
        {' show globe'}
      </label>
      <div style={{width: 400, height: 400}}>
        {showGlobe ? (
          <React.Suspense fallback={<div>Loading Map...</div>}>
            <Globe />
          </React.Suspense>
        ) : null}
      </div>
    </div>
  )
}
// 🦉 Note that if you're not on the isolated page, then you'll notice that this
// app actually already has a React.Suspense component higher up in the tree
// where this component is rendered, so you *could* just rely on that one.

export default App
复制代码

 

Prefetch by webpack:

const loadGlobe = () => import(/* webpackPrefetch: true */ '../globe') // then you don't need to do egar loading anymore
复制代码
      <label style={{marginBottom: '1rem'}}>
        <input
          type="checkbox"
          checked={showGlobe}
          onChange={e => setShowGlobe(e.target.checked)}
        />
        {' show globe'}
      </label>
      <div style={{width: 400, height: 400}}>
        {showGlobe ? (
          <React.Suspense fallback={<div>Loading Map...</div>}>
            <Globe />
          </React.Suspense>
        ) : null}
      </div>
复制代码

Webpack will add those scripts into head.

posted @   Zhentiw  阅读(103)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2019-10-22 [Flutter] How to pass the state between two screen thought router
2018-10-22 [HTML5] Show Different Variations of Images Depending on the Viewport Width using Art Direction
2018-10-22 [HTML5] Show Images of Differing Resolutions Depending on the Viewport Width with srcset
2018-10-22 [NPM] Set default values for package.json using npm set
2018-10-22 [Javascript] Replicate JavaScript Constructor Inheritance with Simple Objects (OLOO)
2018-10-22 [React] Create and import React components with Markdown using MDXC
2017-10-22 [AngularFire] Angular File Uploads to Firebase Storage with Angular control value accessor
点击右上角即可分享
微信分享提示