dynamic-import
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDynamic Import
动态导入
In our chat application, we have four key components: , , and . However, only three of these components are used instantly on the initial page load: , and . The isn't directly visible, and may not even be rendered at all if the user won't even click on the in order to toggle the . This would mean that we unnecessarily added the module to our initial bundle, which potentially increased the loading time!
UserInfoChatListChatInputEmojiPickerUserInfoChatListChatInputEmojiPickerEmojiEmojiPickerEmojiPickerIn order to solve this, we can dynamically import the component. Instead of statically importing it, we'll only import it when we want to show the . An easy way to dynamically import components in React is by using React Suspense. The component receives the component that should be dynamically loaded, which makes it possible for the component to render its contents faster by suspending the import of the module!
EmojiPickerEmojiPickerReact.SuspenseAppEmojiPicker在我们的聊天应用中,有四个核心组件:、、 和 。不过,初始页面加载时仅会立即用到其中三个组件:、 和 。 并非直接可见,而且如果用户根本不会点击「表情」按钮来唤起它,这个组件甚至可能完全不会渲染。这就意味着我们毫无必要地将 模块加入了初始打包文件中,这可能会拖慢页面加载速度!
UserInfoChatListChatInputEmojiPickerUserInfoChatListChatInputEmojiPickerEmojiPicker为了解决这个问题,我们可以动态导入 组件。不再采用静态导入的方式,而是仅在需要显示 时才导入它。在 React 中实现组件动态导入的简便方式是使用 React Suspense。 组件可以包裹需要动态加载的组件,这样 组件就能通过延迟导入 模块,更快地渲染内容!
EmojiPickerEmojiPickerReact.SuspenseAppEmojiPickerWhen to Use
使用场景
- Use this when certain modules are only needed based on user interaction or conditions
- This is helpful when you want to reduce the initial bundle size for faster page loads
- Use this when components like modals, pickers, or heavy libraries aren't needed on initial render
- 当某些模块仅在用户交互或特定条件下才需要时使用
- 适用于希望减小初始打包体积以加快页面加载速度的场景
- 当初始渲染不需要模态框、选择器或大型库等组件时使用
Instructions
操作说明
- Use with
React.lazyfor dynamic component imports in ReactSuspense - Provide meaningful fallback UI while dynamically imported modules are loading
- Consider using for SSR applications where React Suspense isn't supported
loadable-components - Only dynamically import modules that aren't critical to the initial render
- 在 React 中结合 和
React.lazy实现组件动态导入Suspense - 在动态导入模块加载过程中,提供有意义的备用 UI
- 如果是不支持 React Suspense 的 SSR 应用,考虑使用 库
loadable-components - 仅对非初始渲染关键模块进行动态导入
Details
细节说明
Instead of unnecessarily adding to the initial bundle, we can split it up into its own bundle and reduce the size of the initial bundle!
EmojiPickerA smaller initial bundle size means a faster initial load: the user doesn't have to stare at a blank loading screen for as long. The component lets the user know that our application hasn't frozen: they simply need to wait a little while for the module to be processed and executed.
fallbackAsset Size Chunks Chunk Names
emoji-picker.bundle.js 1.48 KiB 1 [emitted] emoji-picker
main.bundle.js 1.33 MiB main [emitted] main
vendors~emoji-picker.bundle.js 171 KiB 2 [emitted] vendors~emoji-pickerWhereas previously the initial bundle was , we've been able to reduce it to by suspending the import of the !
1.5MiB1.33 MiBEmojiPickerBy dynamically importing the component, we managed to reduce the initial bundle size from to ! Although the user may still have to wait a while until the has been fully loaded, we have improved the user experience by making sure the application is rendered and interactive while the user waits for the component to load.
EmojiPicker1.5MiB1.33 MiBEmojiPicker我们无需将 加入初始打包文件,而是可以将它拆分到独立的打包文件中,从而减小初始打包体积!
EmojiPicker更小的初始打包体积意味着更快的初始加载速度:用户不需要长时间盯着空白的加载页面。备用(fallback)组件可以让用户知道应用并未卡死,他们只需稍等片刻,模块就会处理完成并执行。
Asset Size Chunks Chunk Names
emoji-picker.bundle.js 1.48 KiB 1 [emitted] emoji-picker
main.bundle.js 1.33 MiB main [emitted] main
vendors~emoji-picker.bundle.js 171 KiB 2 [emitted] vendors~emoji-picker之前的初始打包体积是 ,通过延迟导入 ,我们将其减小到了 !
1.5MiBEmojiPicker1.33 MiB通过动态导入 组件,我们成功将初始打包体积从 降至 !虽然用户可能仍需等待片刻才能完全加载 ,但我们通过确保应用在组件加载过程中保持可渲染和交互状态,优化了用户体验。
EmojiPicker1.5MiB1.33 MiBEmojiPickerLoadable Components
Loadable Components
Server-side rendering doesn't support React Suspense (yet). A good alternative to React Suspense is the library, which can be used in SSR applications.
loadable-componentsSimilar to React Suspense, we can pass the lazily imported module to the , which will only import the module once the module is being requested! While the module is being loaded, we can render a component.
loadableEmojiPickerfallbackAlthough loadable components are a great alternative to React Suspense for SSR applications, they're also useful in CSR applications in order to suspend the import of modules.
服务端渲染(SSR)目前暂不支持 React Suspense。一个不错的替代方案是 库,它可用于 SSR 应用。
loadable-components与 React Suspense 类似,我们可以将延迟导入的模块传入 ,这样只有当 模块被请求时才会导入它!在模块加载过程中,我们可以渲染一个备用(fallback)组件。
loadableEmojiPicker尽管 Loadable Components 是 SSR 应用中 React Suspense 的优秀替代方案,但它在客户端渲染(CSR)应用中同样实用,可用于延迟导入模块。