react-router-dom之異步加載路由方式
react-router-dom異步加載路由
這篇文章跟http://www.dhdzp.com/article/278887.htm銜接
在一個spa單頁面應(yīng)用中如果項目較小的話異步組件可能影響不大,但是如果是一個大的react單頁面項目如果沒有使用異步組件,頁面會在第一次加載的時候加載所有項目中所有的組件嚴(yán)重影響頁面的加載速度,異步組件可以讓路由跳轉(zhuǎn)到對應(yīng)的路由上才去加載對應(yīng)的react文件,這樣頁面的加載速度就會得到很大的提升。因此異步組件是非常有必要的。
先來看看異步組件是怎么寫的吧
安裝依賴react-loadable
npm install react-loadable
新建一個js文件。異步組件也是要依賴于react所以要引入react。
意思大概就是在loader引入組件完成前先顯示“正在加載”
//入口文件中不在調(diào)用之前的RouterTest組件而是調(diào)用這個異步組件
//獲取路由參數(shù)的方法需要進(jìn)行調(diào)整要在對應(yīng)的再組件中調(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 />既然用了異步組件其他幾個地方就要做相應(yīng)的改動。首先是入口文件
我們將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))
個人認(rèn)為react-router-dom相對vue-router還是比較復(fù)雜的不管是傳參還是異步組件,但是可操作的空間還是有的。
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ī)則放在同一個地方集中式路由,子路由應(yīng)該由父組件動態(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
* 當(dāng)react路由匹配到對應(yīng)路徑,AsyncComponent組件掛在,參數(shù)()=>import('xxx')方法在其掛在后執(zhí)行,將異步加載的組件更改到狀態(tài),并再次重新渲染。
*/總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
React中使用Workbox進(jìn)行預(yù)緩存的實現(xiàn)代碼
Workbox是Google Chrome團(tuán)隊推出的一套 PWA 的解決方案,這套解決方案當(dāng)中包含了核心庫和構(gòu)建工具,因此我們可以利用Workbox實現(xiàn)Service Worker的快速開發(fā),本文小編給大家介紹了React中使用Workbox進(jìn)行預(yù)緩存的實現(xiàn),需要的朋友可以參考下2023-11-11
react數(shù)據(jù)管理機制React.Context源碼解析
這篇文章主要為大家介紹了react數(shù)據(jù)管理機制React.Context源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
React實現(xiàn)二級聯(lián)動效果(樓梯效果)
這篇文章主要為大家詳細(xì)介紹了React實現(xiàn)二級聯(lián)動效果,樓梯效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09
hooks寫React組件的5個注意細(xì)節(jié)詳解
這篇文章主要為大家介紹了hooks寫React組件的5個需要注意的細(xì)節(jié)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

