react-router-dom之異步加載路由方式
react-router-dom異步加載路由
這篇文章跟http://www.dbjr.com.cn/article/278887.htm銜接
在一個spa單頁面應用中如果項目較小的話異步組件可能影響不大,但是如果是一個大的react單頁面項目如果沒有使用異步組件,頁面會在第一次加載的時候加載所有項目中所有的組件嚴重影響頁面的加載速度,異步組件可以讓路由跳轉(zhuǎn)到對應的路由上才去加載對應的react文件,這樣頁面的加載速度就會得到很大的提升。因此異步組件是非常有必要的。
先來看看異步組件是怎么寫的吧
安裝依賴react-loadable
npm install react-loadable
新建一個js文件。異步組件也是要依賴于react所以要引入react。
意思大概就是在loader引入組件完成前先顯示“正在加載”
//入口文件中不在調(diào)用之前的RouterTest組件而是調(diào)用這個異步組件 //獲取路由參數(shù)的方法需要進行調(diào)整要在對應的再組件中調(diào)用react-router-dom中的withRouter方法 //然后export default connect(null,null)(withRouter(RouterTest)) import React from 'react' import Loadable from 'react-loadable' const LoadingRouterTest = Loadable({ loader:() => import('./RouterTest.js'), loading () {return <div>正在加載</div>} }) export default () => <LoadingRouterTest />
既然用了異步組件其他幾個地方就要做相應的改動。首先是入口文件
我們將RouterTest替換成LoadingRouterTest這個組件
<Route path="/LoadingRouterTest:id" component={LoadingRouterTest}></Route> <Route path="/RouterTest:id" component={RouterTest}></Route>
我們跳轉(zhuǎn)也是跳轉(zhuǎn)LoadingRouterTest這個路由url變成這樣,但是頁面還是RouterTest。
這里會發(fā)現(xiàn)一個問題就是路由的參數(shù)獲取不到了
原因是因為url名稱變了但是頁面還是RouterTest
這里需要用到react-router-dom的withRouter
在暴露RouterTest組件的時候使用withRouter這個方法允許RouterTest使用LoadingRouterTest的url參數(shù)
export default connect(null,null)(withRouter(RouterTest))
個人認為react-router-dom相對vue-router還是比較復雜的不管是傳參還是異步組件,但是可操作的空間還是有的。
react路由組件異步加載,優(yōu)化白屏
//手寫異步加載高階組件 import React, { Component } from "react"; export default function asyncComponent(importComponent) { class AsyncComponent extends Component { constructor(props) { super(props); this.state = { component: null }; } async componentDidMount() { const { default: component } = await importComponent(); this.setState({component}); } render() { const C = this.state.component; return C ? <C {...this.props} /> : null; } } return AsyncComponent; }
import React, { Component } from 'react'; import { HashRouter, Switch, Route, Redirect } from 'react-router-dom'; import asyncComponent from '@/utils/asyncComponent'; import home from "@/pages/home/home"; const record = asyncComponent(() => import("@/pages/record/record")); const helpcenter = asyncComponent(() => import("@/pages/helpcenter/helpcenter")); const production = asyncComponent(() => import("@/pages/production/production")); const balance = asyncComponent(() => import("@/pages/balance/balance")); // react-router4 不再推薦將所有路由規(guī)則放在同一個地方集中式路由,子路由應該由父組件動態(tài)配置,組件在哪里匹配就在哪里渲染,更加靈活 export default class RouteConfig extends Component{ render(){ return( <HashRouter> <Switch> <Route path="/" exact component={home} /> <Route path="/record" component={record} /> <Route path="/helpcenter" component={helpcenter} /> <Route path="/production" component={production} /> <Route path="/balance" component={balance} /> <Redirect to="/" /> </Switch> </HashRouter> ) } }
/**注解 * 路由表中: * const record = asyncComponent(() => import("@/pages/record/record")); * <Route path="/record" component={record} /> * -------------------------------------------------------------- * >>1 * asyncComponent函數(shù)返回了一個組件AsyncComponent,AsyncComponent被record變量接收; * asyncComponent函數(shù)參數(shù)是一個()=>import('xxx')方法,該方法是用來引入文件的,返回的的是一個promise對象。 * >>2 * 當react路由匹配到對應路徑,AsyncComponent組件掛在,參數(shù)()=>import('xxx')方法在其掛在后執(zhí)行,將異步加載的組件更改到狀態(tài),并再次重新渲染。 */
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
react將文件轉(zhuǎn)為base64上傳的示例代碼
本文主要介紹了react將文件轉(zhuǎn)為base64上傳的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-09-09使用 React Router Dom 實現(xiàn)路由導航的詳細過程
React Router Dom 是 React 應用程序中用于處理路由的常用庫,它提供了一系列組件和 API 來管理應用程序的路由,這篇文章主要介紹了使用 React Router Dom 實現(xiàn)路由導航,需要的朋友可以參考下2024-03-03react清空ant.design中表單內(nèi)容的方法實現(xiàn)
本文主要介紹了react清空ant.design中表單內(nèi)容的方法實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-12-12React實現(xiàn)下拉框的key,value的值同時傳送
這篇文章主要介紹了React實現(xiàn)下拉框的key,value的值同時傳送方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08