摘要:依賴(lài)于一概念理解痛點(diǎn)界面需要導(dǎo)航,需要根據(jù)不同的導(dǎo)航加載不同的模塊需要處理的問(wèn)題地址欄按需加載不同的模塊適當(dāng)進(jìn)行個(gè)性化處理其實(shí),上面兩個(gè)問(wèn)題,并不難處理,但是本著抽象的原則,需要抽象出一個(gè)通用的模塊。提供了一個(gè)良好的機(jī)制進(jìn)行處理。
React-router依賴(lài)于:history
一、概念理解痛點(diǎn):界面需要導(dǎo)航,需要根據(jù)不同的導(dǎo)航加載不同的模塊 需要處理的問(wèn)題: 1、URL地址欄 2、按需加載不同的模塊(適當(dāng)進(jìn)行個(gè)性化處理) 其實(shí),上面兩個(gè)問(wèn)題,并不難處理,但是本著抽象的原則,需要抽象出一個(gè)通用的模塊。 React-router提供了一個(gè)良好的機(jī)制進(jìn)行處理。二、簡(jiǎn)單Demo
三、源碼解析 1、Router和Route)
????Route:匹配路徑,并進(jìn)行渲染
//代碼是部分代碼模塊,如果想看真的源碼,移步github class Route extends React.Component(){ constructor(){ this.state={ match:this.computeMatch(this.props, this.context.router) } } //這部分的目的主要是讓大家了解下源碼中需要輸入的參數(shù)和類(lèi)型 static propTypes = { computedMatch: PropTypes.object, // private, frompath: PropTypes.string, exact: PropTypes.bool, strict: PropTypes.bool, sensitive: PropTypes.bool, component: PropTypes.func, render: PropTypes.func, children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]), location: PropTypes.object }; //每次路由發(fā)生變化,就進(jìn)行重新渲染 componentWillReceiveProps(nextProps,nextContext){ this.setState({ match: this.computeMatch(nextProps, nextContext.router) }); } render(){ //返回 if (component) return match ? React.createElement(component, props) : null; } }
????Router:把history放入全局的context里面
class Router extends React.Component { //輸入?yún)?shù)多了history static propTypes = { history: PropTypes.object.isRequired, children: PropTypes.node }; //大部分都一樣,render返回不一樣 render() { const { children } = this.props; return children ? React.Children.only(children) : null; } }2、Switch、Redirect
//switch核心模塊 let match, child; React.Children.forEach(children, element => { if (match == null && React.isValidElement(element)) { const { path: pathProp, exact, strict, sensitive, from } = element.props; const path = pathProp || from; child = element; match = matchPath( location.pathname, { path, exact, strict, sensitive }, route.match ); } }); return match ? React.cloneElement(child, { location, computedMatch: match }) : null; //Redirect核心模塊 componentDidUpdate(prevProps) { const prevTo = createLocation(prevProps.to); const nextTo = createLocation(this.props.to); this.perform(); } computeTo({ computedMatch, to }) { if (computedMatch) { if (typeof to === "string") { return generatePath(to, computedMatch.params); } else { return { ...to, pathname: generatePath(to.pathname, computedMatch.params) }; } } return to; } perform() { const { history } = this.context.router; const { push } = this.props; const to = this.computeTo(this.props); if (push) { history.push(to); } else { history.replace(to); } }3、HashRouter、BrowserRouter:以Router為基礎(chǔ)
//HashRouter源碼很簡(jiǎn)單 import { createHashHistory as createHistory } from "history"; class HashRouter extends React.Component { static propTypes = { basename: PropTypes.string, getUserConfirmation: PropTypes.func, hashType: PropTypes.oneOf(["hashbang", "noslash", "slash"]), children: PropTypes.node }; history = createHistory(this.props); render() { return; } } //BrowserRouter源碼也很簡(jiǎn)單 import { createBrowserHistory as createHistory } from "history"; class BrowserRouter extends React.Component { static propTypes = { basename: PropTypes.string, forceRefresh: PropTypes.bool, getUserConfirmation: PropTypes.func, keyLength: PropTypes.number, children: PropTypes.node }; history = createHistory(this.props); render() { return ; } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/96428.html
摘要:前言最近將公司項(xiàng)目的從版本升到了版本,跟完全不兼容,是一次徹底的重寫(xiě)。升級(jí)過(guò)程中踩了不少的坑,也有一些值得分享的點(diǎn)。沒(méi)有就會(huì)匹配所有路由最后不得不說(shuō)升級(jí)很困難,坑也很多。 前言 最近將公司項(xiàng)目的 react-router 從 v3 版本升到了 v4 版本,react-router v4 跟 v3 完全不兼容,是一次徹底的重寫(xiě)。這也給升級(jí)造成了極大的困難,與其說(shuō)升級(jí)不如說(shuō)是對(duì) route...
摘要:概述相對(duì)于幾乎是重寫(xiě)了新版的更偏向于組件化。汲取了很多思想,路由即是組件,使路由更具聲明式,且方便組合。如果你習(xí)慣使用,那么一定會(huì)很快上手新版的。被一分為三。不止是否有意義參考資料遷移到關(guān)注點(diǎn)官方文檔 概述 react-router V4 相對(duì)于react-router V2 or V3 幾乎是重寫(xiě)了, 新版的react-router更偏向于組件化(everything is comp...
摘要:解決了什么問(wèn)題首先,它搭配組件,可以組織組件結(jié)構(gòu)代碼,授權(quán)路由的控制。有兩種方式核心的各自做了什么事首先它們都是基于構(gòu)建,也就是的組件的路由,至于各自做了什么。去看官方文檔,里面描述了每一個(gè)組件路由做了什么事。 React-Router解決了什么問(wèn)題? 首先,它搭配React組件,可以組織React組件結(jié)構(gòu)代碼,授權(quán)路由的控制。應(yīng)該展示什么樣的組件,通過(guò)React-Router去匹配它...
摘要:中的包中的包主要有三個(gè)和。的理念上面提到的理念是一切皆組件以下統(tǒng)一稱(chēng)組件。從這點(diǎn)來(lái)說(shuō)的確方便了不少,也迎合一切皆組件的理念。組件是中主要的組成單位,可以認(rèn)為是或的路由入口。將該標(biāo)示為嚴(yán)格匹配路由。的屬性追加一條。 2019年不知不覺(jué)已經(jīng)過(guò)去19天了,有沒(méi)有給自己做個(gè)總結(jié)?有沒(méi)有給明年做個(gè)計(jì)劃?當(dāng)然筆者已經(jīng)做好了明年的工作、學(xué)習(xí)計(jì)劃;同時(shí)也包括該系列博客剩下的博文計(jì)劃,目前還剩4篇:分別...
摘要:回調(diào)函數(shù)將在更新時(shí)觸發(fā),回調(diào)中的起到了新的的作用。注冊(cè)回調(diào)在中使用注冊(cè)的回調(diào)函數(shù),最終放在模塊的回調(diào)函數(shù)數(shù)組中。 原文地址:https://github.com/joeyguo/blog/issues/2 在單頁(yè)應(yīng)用上,前端路由并不陌生。很多前端框架也會(huì)有獨(dú)立開(kāi)發(fā)或推薦配套使用的路由系統(tǒng)。那么,當(dāng)我們?cè)谡勄岸寺酚傻臅r(shí)候,還可以談些什么?本文將簡(jiǎn)要分析并實(shí)現(xiàn)一個(gè)的前端路由,并對(duì) reac...
摘要:通過(guò)前端路由可以實(shí)現(xiàn)單頁(yè)應(yīng)用本文首先從前端路由的原理出發(fā),詳細(xì)介紹了前端路由原理的變遷。接著從的源碼出發(fā),深入理解是如何實(shí)現(xiàn)前端路由的。執(zhí)行上述的賦值后,頁(yè)面的發(fā)生改變。 ??react-router等前端路由的原理大致相同,可以實(shí)現(xiàn)無(wú)刷新的條件下切換顯示不同的頁(yè)面。路由的本質(zhì)就是頁(yè)面的URL發(fā)生改變時(shí),頁(yè)面的顯示結(jié)果可以根據(jù)URL的變化而變化,但是頁(yè)面不會(huì)刷新。通過(guò)前端路由可以實(shí)現(xiàn)...
閱讀 3301·2021-10-11 10:59
閱讀 2863·2021-10-11 10:58
閱讀 2276·2021-09-04 16:45
閱讀 2759·2019-08-30 15:44
閱讀 703·2019-08-30 15:44
閱讀 3225·2019-08-30 10:51
閱讀 1621·2019-08-29 18:46
閱讀 2781·2019-08-29 13:57