自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

【翻譯】基于 Create React App路由4.0的異步組件加載(Code Splitting)

開發(fā) 前端
本文章是一個額外的篇章,它可以在你的React app中,幫助加快初始的加載組件時間。當然這個操作不是完全必要的,但如果你好奇的話,請隨意跟隨這篇文章一起用Create React App和 react路由4.0的異步加載方式來幫助react.js構(gòu)建大型應用。

基于 Create React App路由4.0的異步組件加載

本文章是一個額外的篇章,它可以在你的React app中,幫助加快初始的加載組件時間。當然這個操作不是完全必要的,但如果你好奇的話,請隨意跟隨這篇文章一起用Create React App和 react路由4.0的異步加載方式來幫助react.js構(gòu)建大型應用。

代碼分割(Code Splitting)

當我們用react.js寫我們的單頁應用程序時候,這個應用會變得越來越大,一個應用(或者路由頁面)可能會引入大量的組件,可是有些組件是***次加載的時候是不必要的,這些不必要的組件會浪費很多的加載時間。

你可能會注意到 Create React App 在打包完畢之后會生成一個很大的.js文件,這包含了我們應用程序需要的所有JavaScript。但是,如果用戶只是加載登錄頁面去登錄網(wǎng)站,我們加載應用程序的其余部分是沒有意義的。在我們的應用程序還很小的時候,這并不是一個問題,但是它卻是我們程序猿優(yōu)化的一個東西。為了解決這個問題,Create React App有一個非常簡單的代碼分割的的方案。

代碼分割和 react-router

在我們 react app 中,常見的路由配置可能是像下面一樣的

  1. /* Import the components */ 
  2. import Home from './containers/Home'
  3. import Posts from './containers/Posts'
  4. import NotFound from './containers/NotFound'
  5.  
  6.  
  7. /* Use components to define routes */ 
  8. export default () => ( 
  9.   <Switch> 
  10.     <Route path="/" exact component={Home} /> 
  11.     <Route path="/posts/:id" exact component={Posts} /> 
  12.     <Route component={NotFound} /> 
  13.   </Switch> 
  14. );  

我們一開始引入這些組件,然后定義好的路徑,會根據(jù)我們的路由去匹配這些組件。

但是,我們靜態(tài)地在頂部導入路由中的所有組件。這意味著,不管哪個路由匹配,所有這些組件都被加載。我們只想加載對匹配路由的時候才加載響應的組件。下面我們一步步來完成這個使命。

創(chuàng)建一個異步組件

創(chuàng)建一個js 文件,如:src/components/AsyncComponent.js,代碼如下

  1. import React, { Component } from 'react'
  2.  
  3. export default function asyncComponent(importComponent) { 
  4.  
  5.   class AsyncComponent extends Component { 
  6.  
  7.     constructor(props) { 
  8.       super(props); 
  9.  
  10.       this.state = { 
  11.         component: null
  12.       }; 
  13.     } 
  14.  
  15.     async componentDidMount() { 
  16.       const { default: component } = await importComponent(); 
  17.  
  18.       this.setState({ 
  19.         component: component 
  20.       }); 
  21.     } 
  22.  
  23.     render() { 
  24.       const C = this.state.component; 
  25.  
  26.       return C 
  27.         ? <C {...this.props} /> 
  28.         : null
  29.     } 
  30.  
  31.   } 
  32.  
  33.   return AsyncComponent; 
  34.  

我們在這里做了一些事情:

  1. 這個asyncComponent 函數(shù)接受一個importComponent 的參數(shù),importComponent 調(diào)用時候?qū)討B(tài)引入給定的組件。
  2. 在componentDidMount 我們只是簡單地調(diào)用importComponent 函數(shù),并將動態(tài)加載的組件保存在狀態(tài)中。
  3. ***,如果完成渲染,我們有條件地提供組件。在這里我們?nèi)绻粚憂ull的話,也可提供一個菊花圖,代表著組件正在渲染。

使用異步組件

現(xiàn)在讓我們使用我們的異步組件,而不是像開始的靜態(tài)去引入。

  1. import Home from './containers/Home'

我們要用asyncComponent組件來動態(tài)引入我們需要的組件。

tip: 別忘記 先 import asyncComponent from './AsyncComponent

  1. const AsyncHome = asyncComponent(() => import('./containers/Home')); 

我們將要使用 AsyncHome 這個組件在我們的路由里面

  1. <Route path="/" exact component={AsyncHome} /> 

現(xiàn)在讓我們回到Notes項目并應用這些更改。

src/Routes.js

  1. import React from 'react'
  2. import { Route, Switch } from 'react-router-dom'
  3. import asyncComponent from './components/AsyncComponent'
  4. import AppliedRoute from './components/AppliedRoute'
  5. import AuthenticatedRoute from './components/AuthenticatedRoute'
  6. import UnauthenticatedRoute from './components/UnauthenticatedRoute'
  7.  
  8. const AsyncHome     = asyncComponent(() => import('./containers/Home')); 
  9. const AsyncLogin    = asyncComponent(() => import('./containers/Login')); 
  10. const AsyncNotes    = asyncComponent(() => import('./containers/Notes')); 
  11. const AsyncSignup   = asyncComponent(() => import('./containers/Signup')); 
  12. const AsyncNewNote  = asyncComponent(() => import('./containers/NewNote')); 
  13. const AsyncNotFound = asyncComponent(() => import('./containers/NotFound')); 
  14.  
  15. export default ({ childProps }) => ( 
  16.   <Switch> 
  17.     <AppliedRoute path="/" exact component={AsyncHome} props={childProps} /> 
  18.     <UnauthenticatedRoute path="/login" exact component={AsyncLogin} props={childProps} /> 
  19.     <UnauthenticatedRoute path="/signup" exact component={AsyncSignup} props={childProps} /> 
  20.     <AuthenticatedRoute path="/notes/new" exact component={AsyncNewNote} props={childProps} /> 
  21.     <AuthenticatedRoute path="/notes/:id" exact component={AsyncNotes} props={childProps} /> 
  22.     { /* Finally, catch all unmatched routes */ } 
  23.     <Route component={AsyncNotFound} /> 
  24.   </Switch> 
  25. );  

只需幾次更改就相當酷了。我們的app都是設置了代碼分割而的。也沒有增加太多的復雜性。

這里我們看看之前的這個src/Routes.js路由文件

  1. import React from 'react'
  2. import { Route, Switch } from 'react-router-dom'
  3. import AppliedRoute from './components/AppliedRoute'
  4. import AuthenticatedRoute from './components/AuthenticatedRoute'
  5. import UnauthenticatedRoute from './components/UnauthenticatedRoute'
  6.  
  7. import Home from './containers/Home'
  8. import Login from './containers/Login'
  9. import Notes from './containers/Notes'
  10. import Signup from './containers/Signup'
  11. import NewNote from './containers/NewNote'
  12. import NotFound from './containers/NotFound'
  13.  
  14. export default ({ childProps }) => ( 
  15.   <Switch> 
  16.     <AppliedRoute path="/" exact component={Home} props={childProps} /> 
  17.     <UnauthenticatedRoute path="/login" exact component={Login} props={childProps} /> 
  18.     <UnauthenticatedRoute path="/signup" exact component={Signup} props={childProps} /> 
  19.     <AuthenticatedRoute path="/notes/new" exact component={NewNote} props={childProps} /> 
  20.     <AuthenticatedRoute path="/notes/:id" exact component={Notes} props={childProps} /> 
  21.     { /* Finally, catch all unmatched routes */ } 
  22.     <Route component={NotFound} /> 
  23.   </Switch> 
  24. );  

注意,不要在頂部的引入所有的組件。我們正在創(chuàng)建這些代碼分割的功能,以便在必要時為我們進行動態(tài)導入。

現(xiàn)在你運行npm run build 您將看到代碼已經(jīng)被分割成一個個小文件。

 

下面是部署好的在網(wǎng)站的真實截圖 

 

每個.chunk.js都是需要的時候才加載的。當然我們的程序是相當小的,并且分離在各個部分的小組件,是不需要這樣子按需加載的。還是看你項目的需求。 

責任編輯:龐桂玉 來源: segmentfault
相關(guān)推薦

2017-03-28 10:11:12

Webpack 2React加載

2023-02-02 08:41:14

React團隊Vite

2025-02-17 05:00:00

工具項目Cursor

2021-06-07 08:41:59

React異步組件

2021-07-01 07:03:32

開發(fā)Webpack代碼

2025-02-17 12:24:06

2024-01-30 08:30:41

TypeScript編譯器類型

2021-11-07 20:43:14

React

2022-06-13 06:20:42

setStatereact18

2022-07-10 20:45:47

React加載動畫庫

2023-03-22 23:23:25

React加載動畫庫

2021-10-13 14:01:00

函數(shù)React進階

2021-09-01 19:33:41

Source SentryDocker

2022-02-04 22:18:28

React路由應用

2013-04-01 15:25:41

異步編程異步EMP

2020-09-24 08:45:10

React架構(gòu)源碼

2024-03-13 08:37:18

Vue3Suspense異步組件

2023-06-29 07:48:35

異步加載JavaScript

2013-04-10 17:52:15

微信公眾平臺接口開發(fā)

2019-07-20 23:30:48

開發(fā)技能代碼
點贊
收藏

51CTO技術(shù)棧公眾號