偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

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

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

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

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

代碼分割(Code Splitting)

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

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

代碼分割和 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. );  

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

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

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

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

使用異步組件

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

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

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

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

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

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

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

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

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. );  

只需幾次更改就相當(dāng)酷了。我們的app都是設(shè)置了代碼分割而的。也沒有增加太多的復(fù)雜性。

這里我們看看之前的這個(gè)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)建這些代碼分割的功能,以便在必要時(shí)為我們進(jìn)行動(dòng)態(tài)導(dǎo)入。

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

 

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

 

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

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

2017-03-28 10:11:12

Webpack 2React加載

2023-02-02 08:41:14

React團(tuán)隊(duì)Vite

2025-02-17 05:00:00

工具項(xiàng)目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-10-13 14:01:00

函數(shù)React進(jìn)階

2021-11-07 20:43:14

React

2024-03-13 08:37:18

Vue3Suspense異步組件

2022-06-13 06:20:42

setStatereact18

2017-02-28 21:57:05

React組件

2022-07-10 20:45:47

React加載動(dòng)畫庫

2023-03-22 23:23:25

React加載動(dòng)畫庫

2019-07-20 23:30:48

開發(fā)技能代碼

2019-07-22 10:42:11

React組件前端

2024-03-20 09:31:00

圖片懶加載性能優(yōu)化React

2022-02-04 22:18:28

React路由應(yīng)用

2021-09-01 19:33:41

Source SentryDocker

2013-04-01 15:25:41

異步編程異步EMP
點(diǎn)贊
收藏

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