網站首頁 編程語言 正文
代碼分割
- 打包是個很棒的技術,但隨著代碼量的增加,容易出現體積過大而導致加載時間過長。為避免這種情況的出現,在前期就思考該問題并對代碼包進行分割是個不錯的選擇。
- 對應用進行代碼分割能夠幫助你“懶加載”當前用戶所需要的內容,提高應用性能。盡管并沒有減少應用整體的代碼體積,但可以避免加載用戶不需要的代碼,并在初始加載的時候減少所需加載的代碼量。
- 簡單理解:是性能優化的一種解決方案。比如我們的路由系統中有100個組件,我們在react項目啟動時不需要對這100個組件都進行編譯,只編譯當前URL對應的組件,這就可以用到代碼分割的功能;
React.lazy&Suspense
- 動態引入
//使用前:
import OtherComponent from './OtherComponent';
//使用之后:
const OtherComponent = React.lazy(() => import('./OtherComponent'))
- 在組件首次渲染時,自動導入包含該組件的包
- React.lazy 接受一個函數,這個函數需要動態調用import()。它必須返回一個Promist,該Promise 需要resolve 一個default export 的react組件
- 然后應在 Suspense 組件中渲染 lazy 組件,這樣可以在等待加載 lazy 組件時做優雅降級(如 loading 指示器等)
import React, { Suspense } from 'react';
const OtherComponent = React.lazy(() => import('./OtherComponent'))
const AnotherComponent = React.lazy(() => import('./AnotherComponent'))
function MyComponent() {
return (
<div>
<Suspense fallback={<div>Loading...</div>}>
<section>
<OtherComponent />
<AnotherComponent />
</section>
</Suspense>
</div>
);
}
- fallback:接受任何在組件加載過程中你想展示的 React 元素
- Suspense 組件置于懶加載組件之上的任何位置
異常捕獲邊界(Error boundaries)
如果模塊加載失?。ㄈ缇W絡問題),它會觸發一個錯誤??梢酝ㄟ^異常捕獲邊界(Error boundaries)技術來處理這些情況,以顯示良好的用戶體驗并管理恢復事宜。
import React, { Suspense } from 'react';
import MyErrorBoundary from './MyErrorBoundary';
const OtherComponent = React.lazy(() => import('./OtherComponent'));
const AnotherComponent = React.lazy(() => import('./AnotherComponent'));
const MyComponent = () => (
<div>
<MyErrorBoundary>
<Suspense fallback={<div>Loading...</div>}>
<section>
<OtherComponent />
<AnotherComponent />
</section>
</Suspense>
</MyErrorBoundary>
</div>
);
基于路由的代碼分割
可以選擇從路由開始實現代碼分割。
import React, { Suspense, lazy } from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
const Home = lazy(() => import('./routes/Home'));
const About = lazy(() => import('./routes/About'));
const App = () => (
<Router>
<Suspense fallback={<div>Loading...</div>}>
<Switch>
<Route exact path="/" component={Home}/>
<Route path="/about" component={About}/>
</Switch>
</Suspense>
</Router>
);
命名導出(Named Exports)
- React.lazy 目前只支持默認導出(default exports)
- 若導出其他模塊,可以創建一個中間模塊,來重新導出為默認模塊
// ManyComponents.js
export const MyComponent = /* ... */;
export const MyUnusedComponent = /* ... */;
// MyComponent.js
export { MyComponent as default } from "./ManyComponents.js";
// MyApp.js
import React, { lazy } from 'react';
const MyComponent = lazy(() => import("./MyComponent.js"));
原文鏈接:https://blog.csdn.net/xbczrz/article/details/127925688
相關推薦
- 2022-11-14 redux與react-redux的學習筆記之react-redux
- 2022-04-10 Blazor組件事件處理功能_基礎應用
- 2022-08-30 C語言深入詳解四大內存函數的使用_C 語言
- 2023-07-07 maven查看jar的pom引入來源
- 2022-05-15 Python?文本文件與csv文件的讀取與寫入_python
- 2022-07-08 C#中的Dialog對話框_C#教程
- 2023-05-08 C語言中隊列的結構和函數接口的使用示例_C 語言
- 2022-06-07 ASP.NET?Core服務生命周期_基礎應用
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支