资讯专栏INFORMATION COLUMN

react-router v4.x 源码拾遗1

itvincent / 2018人阅读

摘要:还是先来一段官方的基础使用案例,熟悉一下整体的代码流程中使用了端常用到的等一些常用组件,作为的顶层组件来获取的和设置回调函数来更新。

react-router是react官方推荐并参与维护的一个路由库,支持浏览器端、app端、服务端等常见场景下的路由切换功能,react-router本身不具备切换和跳转路由的功能,这些功能全部由react-router依赖的history库完成,history库通过对url的监听来触发 Router 组件注册的回调,回调函数中会获取最新的url地址和其他参数然后通过setState更新,从而使整个应用进行rerender。所以react-router本身只是封装了业务上的众多功能性组件,比如Route、Link、Redirect 等等,这些组件通过context api可以获取到Router传递history api,比如push、replace等,从而完成页面的跳转。
还是先来一段react-router官方的基础使用案例,熟悉一下整体的代码流程

</>复制代码

  1. import React from "react";
  2. import { BrowserRouter as Router, Route, Link } from "react-router-dom";
  3. function BasicExample() {
  4. return (
    • Home
    • About
    • Topics

  5. );
  6. }
  7. function Home() {
  8. return (
  9. Home

  10. );
  11. }
  12. function About() {
  13. return (
  14. About

  15. );
  16. }
  17. function Topics({ match }) {
  18. return (
  19. Topics

    • Rendering with React
    • Components
    • Props v. State
  20. Please select a topic.

    }
  21. />
  22. );
  23. }
  24. function Topic({ match }) {
  25. return (
  26. {match.params.topicId}

  27. );
  28. }
  29. export default BasicExample;

Demo中使用了web端常用到的BrowserRouter、Route、Link等一些常用组件,Router作为react-router的顶层组件来获取 history 的api 和 设置回调函数来更新state。这里引用的组件都是来自react-router-dom 这个库,那么react-router 和 react-router-dom 是什么关系呢。
说的简单一点,react-router-dom 是对react-router所有组件或方法的一个二次导出,并且在react-router组件的基础上添加了新的组件,更加方便开发者处理复杂的应用业务。

1.react-router 导出的所有内容

统计一下,总共10个方法
1.MemoryRouter.js、2.Prompt.js、3.Redirect.js、4.Route.js、5.Router.js、6.StaticRouter.js、7.Switch.js、8.generatePath.js、9.matchPath.js、10.withRouter.js

2.react-router-dom 导出的所有内容

统计一下,总共14个方法
1.BrowserRouter.js、2.HashRouter.js、3.Link.js、4.MemoryRouter.js、5.NavLink.js、6.Prompt.js、7.Redirect.js、8.Route.js、9.Router.js、10.StaticRouter.js、11.Switch.js、12.generatePath.js、13.matchPath.js、14.withRouter.js
react-router-dom在react-router的10个方法上,又添加了4个方法,分别是BrowserRouter、HashRouter、Link、以及NavLink。
所以,react-router-dom将react-router的10个方法引入后,又加入了4个方法,再重新导出,在开发中我们只需要引入react-router-dom这个依赖即可。

下面进入react-router-dom的源码分析阶段,首先来看一下react-router-dom的依赖库

React, 要求版本大于等于15.x

history, react-router的核心依赖库,注入组件操作路由的api

invariant, 用来抛出异常的工具库

loose-envify, 使用browserify工具进行打包的时候,会将项目当中的node全局变量替换为对应的字符串

prop-types, react的props类型校验工具库

react-router, 依赖同版本的react-router

warning, 控制台打印警告信息的工具库

①.BrowserRouter.js, 提供了HTML5的history api 如pushState、replaceState等来切换地址,源码如下

</>复制代码

  1. import warning from "warning";
  2. import React from "react";
  3. import PropTypes from "prop-types";
  4. import { createBrowserHistory as createHistory } from "history";
  5. import Router from "./Router";
  6. /**
  7. * The public API for a that uses HTML5 history.
  8. */
  9. class BrowserRouter extends React.Component {
  10. static propTypes = {
  11. basename: PropTypes.string, // 当应用为某个子应用时,添加的地址栏前缀
  12. forceRefresh: PropTypes.bool, // 切换路由时,是否强制刷新
  13. getUserConfirmation: PropTypes.func, // 使用Prompt组件时 提示用户的confirm确认方法,默认使用window.confirm
  14. keyLength: PropTypes.number, // 为了实现block功能,react-router维护创建了一个访问过的路由表,每个key代表一个曾经访问过的路由地址
  15. children: PropTypes.node // 子节点
  16. };
  17. // 核心api, 提供了push replace go等路由跳转方法
  18. history = createHistory(this.props);
  19. // 提示用户 BrowserRouter不接受用户自定义的history方法,
  20. // 如果传递了history会被忽略,如果用户使用自定义的history api,
  21. // 需要使用 Router 组件进行替代
  22. componentWillMount() {
  23. warning(
  24. !this.props.history,
  25. " ignores the history prop. To use a custom history, " +
  26. "use `import { Router }` instead of `import { BrowserRouter as Router }`."
  27. );
  28. }
  29. // 将history和children作为props传递给Router组件 并返回
  30. render() {
  31. return ;
  32. }
  33. }
  34. export default BrowserRouter;

**总结:BrowserRouter组件非常简单,它本身其实就是对Router组件的一个包装,将HTML5的history api封装好再赋予 Router 组件。BrowserRouter就好比一个容器组件,由它来决定Router的最终api,这样一个Router组件就可以完成多种api的实现,比如HashRouter、StaticRouter 等,减少了代码的耦合度
②. Router.js, 如果说BrowserRouter是Router的容器组件,为Router提供了html5的history api的数据源,那么Router.js 亦可以看作是子节点的容器组件,它除了接收BrowserRouter提供的history api,最主要的功能就是组件本身会响应地址栏的变化进行setState进而完成react本身的rerender,使应用进行相应的UI切换,源码如下**

</>复制代码

  1. import warning from "warning";
  2. import invariant from "invariant";
  3. import React from "react";
  4. import PropTypes from "prop-types";
  5. /**
  6. * The public API for putting history on context.
  7. */
  8. class Router extends React.Component {
  9. // react-router 4.x依然使用的使react旧版的context API
  10. // react-router 5.x将会作出升级
  11. static propTypes = {
  12. history: PropTypes.object.isRequired,
  13. children: PropTypes.node
  14. };
  15. // 此处是为了能够接收父级容器传递的context router,不过父级很少有传递router的
  16. // 存在的目的是为了方便用户使用这种潜在的方式,来传递自定义的router对象
  17. static contextTypes = {
  18. router: PropTypes.object
  19. };
  20. // 传递给子组件的context api router, 可以通过context上下文来获得
  21. static childContextTypes = {
  22. router: PropTypes.object.isRequired
  23. };
  24. // router 对象的具体值
  25. getChildContext() {
  26. return {
  27. router: {
  28. ...this.context.router,
  29. history: this.props.history, // 路由api等,会在history库进行讲解
  30. route: {
  31. location: this.props.history.location, // 也是history库中的内容
  32. match: this.state.match // 对当前地址进行匹配的结果
  33. }
  34. }
  35. };
  36. }
  37. // Router组件的state,作为一个顶层容器组件维护的state,存在两个目的
  38. // 1.主要目的为了实现自上而下的rerender,url改变的时候match对象会被更新
  39. // 2.Router组件是始终会被渲染的组件,match对象会随时得到更新,并经过context api
  40. // 传递给下游子组件route等
  41. state = {
  42. match: this.computeMatch(this.props.history.location.pathname)
  43. };
  44. // match 的4个参数
  45. // 1.path: 是要进行匹配的路径可以是 "/user/:id" 这种动态路由的模式
  46. // 2.url: 地址栏实际的匹配结果
  47. // 3.parmas: 动态路由所匹配到的参数,如果path是 "/user/:id"匹配到了,那么
  48. // params的内容就是 {id: 某个值}
  49. // 4.isExact: 精准匹配即 地址栏的pathname 和 正则匹配到url是否完全相等
  50. computeMatch(pathname) {
  51. return {
  52. path: "/",
  53. url: "/",
  54. params: {},
  55. isExact: pathname === "/"
  56. };
  57. }
  58. componentWillMount() {
  59. const { children, history } = this.props;
  60. // 当 子节点并非由一个根节点包裹时 抛出错误提示开发者
  61. invariant(
  62. children == null || React.Children.count(children) === 1,
  63. "A may have only one child element"
  64. );
  65. // Do this here so we can setState when a changes the
  66. // location in componentWillMount. This happens e.g. when doing
  67. // server rendering using a .
  68. // 使用history.listen方法,在Router被实例化时注册一个回调事件,
  69. // 即location地址发生改变的时候,会重新setState,进而rerender
  70. // 这里使用willMount而不使用didMount的原因时是因为,服务端渲染时不存在dom,
  71. // 故不会调用didMount的钩子,react将在17版本移除此钩子,那么到时候router应该如何实现此功能?
  72. this.unlisten = history.listen(() => {
  73. this.setState({
  74. match: this.computeMatch(history.location.pathname)
  75. });
  76. });
  77. }
  78. // history参数不允许被更改
  79. componentWillReceiveProps(nextProps) {
  80. warning(
  81. this.props.history === nextProps.history,
  82. "You cannot change "
  83. );
  84. }
  85. // 组件销毁时 解绑history对象中的监听事件
  86. componentWillUnmount() {
  87. this.unlisten();
  88. }
  89. // render的时候使用React.Children.only方法再验证一次
  90. // children 必须是一个由根节点包裹的组件或dom
  91. render() {
  92. const { children } = this.props;
  93. return children ? React.Children.only(children) : null;
  94. }
  95. }
  96. export default Router;

总结:Router组件职责很清晰就是作为容器组件,将上层组件的api进行向下的传递,同时组件本身注册了回调方法,来满足浏览器环境下或者服务端环境下location发生变化时,重新setState,达到组件的rerender。那么history对象到底是怎么实现对地址栏进行监听的,又是如何对location进行push 或者 replace的,这就要看history这个库做了啥。

createBrowserHistory.js 使用html5 history api封装的路由控制器

createHashHistory.js 使用hash方法封装的路由控制器

createMemoryHistory.js 针对native app这种原生应用封装的路由控制器,即在内存中维护一份路由表

createTransitionManager.js 针对路由切换时的相同操作抽离的一个公共方法,路由切换的操作器,拦截器和订阅者都存在于此

DOMUtils.js 针对web端dom操作或判断兼容性的一个工具方法集合

LocationUtils.js 针对location url处理等抽离的一个工具方法的集合

PathUtils.js 用来处理url路径的工具方法集合

这里主要分析createBrowserHistory.js文件

</>复制代码

  1. import warning from "warning"
  2. import invariant from "invariant"
  3. import { createLocation } from "./LocationUtils"
  4. import {
  5. addLeadingSlash,
  6. stripTrailingSlash,
  7. hasBasename,
  8. stripBasename,
  9. createPath
  10. } from "./PathUtils"
  11. import createTransitionManager from "./createTransitionManager"
  12. import {
  13. canUseDOM,
  14. addEventListener,
  15. removeEventListener,
  16. getConfirmation,
  17. supportsHistory,
  18. supportsPopStateOnHashChange,
  19. isExtraneousPopstateEvent
  20. } from "./DOMUtils"
  21. const PopStateEvent = "popstate"
  22. const HashChangeEvent = "hashchange"
  23. const getHistoryState = () => {
  24. // ...
  25. }
  26. /**
  27. * Creates a history object that uses the HTML5 history API including
  28. * pushState, replaceState, and the popstate event.
  29. */
  30. const createBrowserHistory = (props = {}) => {
  31. invariant(
  32. canUseDOM,
  33. "Browser history needs a DOM"
  34. )
  35. const globalHistory = window.history
  36. const canUseHistory = supportsHistory()
  37. const needsHashChangeListener = !supportsPopStateOnHashChange()
  38. const {
  39. forceRefresh = false,
  40. getUserConfirmation = getConfirmation,
  41. keyLength = 6
  42. } = props
  43. const basename = props.basename ? stripTrailingSlash(addLeadingSlash(props.basename)) : ""
  44. const getDOMLocation = (historyState) => {
  45. // ...
  46. }
  47. const createKey = () =>
  48. Math.random().toString(36).substr(2, keyLength)
  49. const transitionManager = createTransitionManager()
  50. const setState = (nextState) => {
  51. // ...
  52. }
  53. const handlePopState = (event) => {
  54. // ...
  55. }
  56. const handleHashChange = () => {
  57. // ...
  58. }
  59. let forceNextPop = false
  60. const handlePop = (location) => {
  61. // ...
  62. }
  63. const revertPop = (fromLocation) => {
  64. // ...
  65. }
  66. const initialLocation = getDOMLocation(getHistoryState())
  67. let allKeys = [ initialLocation.key ]
  68. // Public interface
  69. const createHref = (location) =>
  70. basename + createPath(location)
  71. const push = (path, state) => {
  72. // ...
  73. }
  74. const replace = (path, state) => {
  75. // ...
  76. }
  77. const go = (n) => {
  78. globalHistory.go(n)
  79. }
  80. const goBack = () =>
  81. go(-1)
  82. const goForward = () =>
  83. go(1)
  84. let listenerCount = 0
  85. const checkDOMListeners = (delta) => {
  86. // ...
  87. }
  88. let isBlocked = false
  89. const block = (prompt = false) => {
  90. // ...
  91. }
  92. const listen = (listener) => {
  93. // ...
  94. }
  95. const history = {
  96. length: globalHistory.length,
  97. action: "POP",
  98. location: initialLocation,
  99. createHref,
  100. push,
  101. replace,
  102. go,
  103. goBack,
  104. goForward,
  105. block,
  106. listen
  107. }
  108. return history
  109. }
  110. export default createBrowserHistory

createBrowserHistory.js 总共300+行代码,其原理就是封装了原生的html5 的history api,如pushState,replaceState,当这些事件被触发时会激活subscribe的回调来进行响应。同时也会对地址栏进行监听,当history.go等事件触发history popstate事件时,也会激活subscribe的回调。

由于代码量较多,而且依赖的方法较多,这里将方法分成几个小节来进行梳理,对于依赖的方法先进行简短阐述,当实际调用时在深入源码内部去探究实现细节

1. 依赖的工具方法

</>复制代码

  1. import warning from "warning" // 控制台的console.warn警告
  2. import invariant from "invariant" // 用来抛出异常错误信息
  3. // 对地址参数处理,最终返回一个对象包含 pathname,search,hash,state,key 等参数
  4. import { createLocation } from "./LocationUtils"
  5. import {
  6. addLeadingSlash, // 对传递的pathname添加首部`/`,即 "home" 处理为 "/home",存在首部`/`的不做处理
  7. stripTrailingSlash, // 对传递的pathname去掉尾部的 `/`
  8. hasBasename, // 判断是否传递了basename参数
  9. stripBasename, // 如果传递了basename参数,那么每次需要将pathname中的basename统一去除
  10. createPath // 将location对象的参数生成最终的地址栏路径
  11. } from "./PathUtils"
  12. import createTransitionManager from "./createTransitionManager" // 抽离的路由切换的公共方法
  13. import {
  14. canUseDOM, // 当前是否可使用dom, 即window对象是否存在,是否是浏览器环境下
  15. addEventListener, // 兼容ie 监听事件
  16. removeEventListener, // 解绑事件
  17. getConfirmation, // 路由跳转的comfirm 回调,默认使用window.confirm
  18. supportsHistory, // 当前环境是否支持history的pushState方法
  19. supportsPopStateOnHashChange, // hashChange是否会触发h5的popState方法,ie10、11并不会
  20. isExtraneousPopstateEvent // 判断popState是否时真正有效的
  21. } from "./DOMUtils"
  22. const PopStateEvent = "popstate" // 针对popstate事件的监听
  23. const HashChangeEvent = "hashchange" // 针对不支持history api的浏览器 启动hashchange监听事件
  24. // 返回history的state
  25. const getHistoryState = () => {
  26. try {
  27. return window.history.state || {}
  28. } catch (e) {
  29. // IE 11 sometimes throws when accessing window.history.state
  30. // See https://github.com/ReactTraining/history/pull/289
  31. // IE11 下有时会抛出异常,此处保证state一定返回一个对象
  32. return {}
  33. }
  34. }

creareBrowserHistory的具体实现

</>复制代码

  1. const createBrowserHistory = (props = {}) => {
  2. // 当不在浏览器环境下直接抛出错误
  3. invariant(
  4. canUseDOM,
  5. "Browser history needs a DOM"
  6. )
  7. const globalHistory = window.history // 使用window的history
  8. // 此处注意android 2. 和 4.0的版本并且ua的信息是 mobile safari 的history api是有bug且无法解决的
  9. const canUseHistory = supportsHistory()
  10. // hashChange的时候是否会进行popState操作,ie10、11不会进行popState操作
  11. const needsHashChangeListener = !supportsPopStateOnHashChange()
  12. const {
  13. forceRefresh = false, // 默认切换路由不刷新
  14. getUserConfirmation = getConfirmation, // 使用window.confirm
  15. keyLength = 6 // 默认6位长度随机key
  16. } = props
  17. // addLeadingSlash 添加basename头部的斜杠
  18. // stripTrailingSlash 去掉 basename 尾部的斜杠
  19. // 如果basename存在的话,保证其格式为 ‘/xxx’
  20. const basename = props.basename ? stripTrailingSlash(addLeadingSlash(props.basename)) : ""
  21. const getDOMLocation = (historyState) => {
  22. // 获取history对象的key和state
  23. const { key, state } = (historyState || {})
  24. // 获取当前路径下的pathname,search,hash等参数
  25. const { pathname, search, hash } = window.location
  26. // 拼接一个完整的路径
  27. let path = pathname + search + hash
  28. // 当传递了basename后,所有的pathname必须包含这个basename
  29. warning(
  30. (!basename || hasBasename(path, basename)),
  31. "You are attempting to use a basename on a page whose URL path does not begin " +
  32. "with the basename. Expected path "" + path + "" to begin with "" + basename + ""."
  33. )
  34. // 去掉path当中的basename
  35. if (basename)
  36. path = stripBasename(path, basename)
  37. // 生成一个自定义的location对象
  38. return createLocation(path, state, key)
  39. }
  40. // 使用6位长度的随机key
  41. const createKey = () =>
  42. Math.random().toString(36).substr(2, keyLength)
  43. // transitionManager是history中最复杂的部分,复杂的原因是因为
  44. // 为了实现block方法,做了对路由拦截的hack,虽然能实现对路由切时的拦截功能
  45. // 比如Prompt组件,但同时也带来了不可解决的bug,后面在讨论
  46. // 这里返回一个对象包含 setPrompt、confirmTransitionTo、appendListener
  47. // notifyListeners 等四个方法
  48. const transitionManager = createTransitionManager()
  49. const setState = (nextState) => {
  50. // nextState包含最新的 action 和 location
  51. // 并将其更新到导出的 history 对象中,这样Router组件相应的也会得到更新
  52. // 可以理解为同react内部所做的setState时相同的功能
  53. Object.assign(history, nextState)
  54. // 更新history的length, 实实保持和window.history.length 同步
  55. history.length = globalHistory.length
  56. // 通知subscribe进行回调
  57. transitionManager.notifyListeners(
  58. history.location,
  59. history.action
  60. )
  61. }
  62. // 当监听到popState事件时进行的处理
  63. const handlePopState = (event) => {
  64. // Ignore extraneous popstate events in WebKit.
  65. if (isExtraneousPopstateEvent(event))
  66. return
  67. // 获取当前地址栏的history state并传递给getDOMLocation
  68. // 返回一个新的location对象
  69. handlePop(getDOMLocation(event.state))
  70. }
  71. const handleHashChange = () => {
  72. // 监听到hashchange时进行的处理,由于hashchange不会更改state
  73. // 故此处不需要更新location的state
  74. handlePop(getDOMLocation(getHistoryState()))
  75. }
  76. // 用来判断路由是否需要强制
  77. let forceNextPop = false
  78. // handlePop是对使用go方法来回退或者前进时,对页面进行的更新,正常情况下来说没有问题
  79. // 但是如果页面使用Prompt,即路由拦截器。当点击回退或者前进就会触发histrory的api,改变了地址栏的路径
  80. // 然后弹出需要用户进行确认的提示框,如果用户点击确定,那么没问题因为地址栏改变的地址就是将要跳转到地址
  81. // 但是如果用户选择了取消,那么地址栏的路径已经变成了新的地址,但是页面实际还停留再之前,这就产生了bug
  82. // 这也就是 revertPop 这个hack的由来。因为页面的跳转可以由程序控制,但是如果操作的本身是浏览器的前进后退
  83. // 按钮,那么是无法做到真正拦截的。
  84. const handlePop = (location) => {
  85. if (forceNextPop) {
  86. forceNextPop = false
  87. setState()
  88. } else {
  89. const action = "POP"
  90. transitionManager.confirmTransitionTo(location, action, getUserConfirmation, (ok) => {
  91. if (ok) {
  92. setState({ action, location })
  93. } else {
  94. // 当拦截器返回了false的时候,需要把地址栏的路径重置为当前页面显示的地址
  95. revertPop(location)
  96. }
  97. })
  98. }
  99. }
  100. // 这里是react-router的作者最头疼的一个地方,因为虽然用hack实现了表面上的路由拦截
  101. // ,但也会引起一些特殊情况下的bug。这里先说一下如何做到的假装拦截,因为本身html5 history
  102. // api的特性,pushState 这些操作不会引起页面的reload,所有做到拦截只需要不手懂调用setState页面不进行render即可
  103. // 当用户选择了取消后,再将地址栏中的路径变为当前页面的显示路径即可,这也是revertPop实现的方式
  104. // 这里贴出一下对这个bug的讨论:https://github.com/ReactTraining/history/issues/690
  105. const revertPop = (fromLocation) => {
  106. // fromLocation 当前地址栏真正的路径,而且这个路径一定是存在于history历史
  107. // 记录当中某个被访问过的路径,因为我们需要将地址栏的这个路径重置为页面正在显示的路径地址
  108. // 页面显示的这个路径地址一定是还再history.location中的那个地址
  109. // fromLoaction 用户原本想去但是后来又不去的那个地址,需要把他换位history.location当中的那个地址
  110. const toLocation = history.location
  111. // TODO: We could probably make this more reliable by
  112. // keeping a list of keys we"ve seen in sessionStorage.
  113. // Instead, we just default to 0 for keys we don"t know.
  114. // 取出toLocation地址再allKeys中的下标位置
  115. let toIndex = allKeys.indexOf(toLocation.key)
  116. if (toIndex === -1)
  117. toIndex = 0
  118. // 取出formLoaction地址在allKeys中的下标位置
  119. let fromIndex = allKeys.indexOf(fromLocation.key)
  120. if (fromIndex === -1)
  121. fromIndex = 0
  122. // 两者进行相减的值就是go操作需要回退或者前进的次数
  123. const delta = toIndex - fromIndex
  124. // 如果delta不为0,则进行地址栏的变更 将历史记录重定向到当前页面的路径
  125. if (delta) {
  126. forceNextPop = true // 将forceNextPop设置为true
  127. // 更改地址栏的路径,又会触发handlePop 方法,此时由于forceNextPop已经为true则会执行后面的
  128. // setState方法,对当前页面进行rerender,注意setState是没有传递参数的,这样history当中的
  129. // location对象依然是之前页面存在的那个loaction,不会改变history的location数据
  130. go(delta)
  131. }
  132. }
  133. // 返回一个location初始对象包含
  134. // pathname,search,hash,state,key key有可能是undefined
  135. const initialLocation = getDOMLocation(getHistoryState())
  136. let allKeys = [ initialLocation.key ]
  137. // Public interface
  138. // 拼接上basename
  139. const createHref = (location) =>
  140. basename + createPath(location)
  141. const push = (path, state) => {
  142. warning(
  143. !(typeof path === "object" && path.state !== undefined && state !== undefined),
  144. "You should avoid providing a 2nd state argument to push when the 1st " +
  145. "argument is a location-like object that already has state; it is ignored"
  146. )
  147. const action = "PUSH"
  148. const location = createLocation(path, state, createKey(), history.location)
  149. transitionManager.confirmTransitionTo(location, action, getUserConfirmation, (ok) => {
  150. if (!ok)
  151. return
  152. const href = createHref(location) // 拼接basename
  153. const { key, state } = location
  154. if (canUseHistory) {
  155. globalHistory.pushState({ key, state }, null, href) // 只是改变地址栏路径 此时页面不会改变
  156. if (forceRefresh) {
  157. window.location.href = href // 强制刷新
  158. } else {
  159. const prevIndex = allKeys.indexOf(history.location.key) // 上次访问的路径的key
  160. const nextKeys = allKeys.slice(0, prevIndex === -1 ? 0 : prevIndex + 1)
  161. nextKeys.push(location.key) // 维护一个访问过的路径的key的列表
  162. allKeys = nextKeys
  163. setState({ action, location }) // render页面
  164. }
  165. } else {
  166. warning(
  167. state === undefined,
  168. "Browser history cannot push state in browsers that do not support HTML5 history"
  169. )
  170. window.location.href = href
  171. }
  172. })
  173. }
  174. const replace = (path, state) => {
  175. warning(
  176. !(typeof path === "object" && path.state !== undefined && state !== undefined),
  177. "You should avoid providing a 2nd state argument to replace when the 1st " +
  178. "argument is a location-like object that already has state; it is ignored"
  179. )
  180. const action = "REPLACE"
  181. const location = createLocation(path, state, createKey(), history.location)
  182. transitionManager.confirmTransitionTo(location, action, getUserConfirmation, (ok) => {
  183. if (!ok)
  184. return
  185. const href = createHref(location)
  186. const { key, state } = location
  187. if (canUseHistory) {
  188. globalHistory.replaceState({ key, state }, null, href)
  189. if (forceRefresh) {
  190. window.location.replace(href)
  191. } else {
  192. const prevIndex = allKeys.indexOf(history.location.key)
  193. if (prevIndex !== -1)
  194. allKeys[prevIndex] = location.key
  195. setState({ action, location })
  196. }
  197. } else {
  198. warning(
  199. state === undefined,
  200. "Browser history cannot replace state in browsers that do not support HTML5 history"
  201. )
  202. window.location.replace(href)
  203. }
  204. })
  205. }
  206. const go = (n) => {
  207. globalHistory.go(n)
  208. }
  209. const goBack = () =>
  210. go(-1)
  211. const goForward = () =>
  212. go(1)
  213. let listenerCount = 0
  214. // 防止重复注册监听,只有listenerCount == 1的时候才会进行监听事件
  215. const checkDOMListeners = (delta) => {
  216. listenerCount += delta
  217. if (listenerCount === 1) {
  218. addEventListener(window, PopStateEvent, handlePopState)
  219. if (needsHashChangeListener)
  220. addEventListener(window, HashChangeEvent, handleHashChange)
  221. } else if (listenerCount === 0) {
  222. removeEventListener(window, PopStateEvent, handlePopState)
  223. if (needsHashChangeListener)
  224. removeEventListener(window, HashChangeEvent, handleHashChange)
  225. }
  226. }
  227. // 默认情况下不会阻止路由的跳转
  228. let isBlocked = false
  229. // 这里的block方法专门为Prompt组件设计,开发者可以模拟对路由的拦截
  230. const block = (prompt = false) => {
  231. // prompt 默认为false, prompt可以为string或者func
  232. // 将拦截器的开关打开,并返回可关闭拦截器的方法
  233. const unblock = transitionManager.setPrompt(prompt)
  234. // 监听事件只会当拦截器开启时被注册,同时设置isBlock为true,防止多次注册
  235. if (!isBlocked) {
  236. checkDOMListeners(1)
  237. isBlocked = true
  238. }
  239. // 返回关闭拦截器的方法
  240. return () => {
  241. if (isBlocked) {
  242. isBlocked = false
  243. checkDOMListeners(-1)
  244. }
  245. return unblock()
  246. }
  247. }
  248. const listen = (listener) => {
  249. const unlisten = transitionManager.appendListener(listener) // 添加订阅者
  250. checkDOMListeners(1) // 监听popState pushState 等事件
  251. return () => {
  252. checkDOMListeners(-1)
  253. unlisten()
  254. }
  255. }
  256. const history = {
  257. length: globalHistory.length,
  258. action: "POP",
  259. location: initialLocation,
  260. createHref,
  261. push,
  262. replace,
  263. go,
  264. goBack,
  265. goForward,
  266. block,
  267. listen
  268. }
  269. return history
  270. }

由于篇幅过长,所以这里抽取push方法来梳理整套流程

</>复制代码

  1. const push = (path, state) => {
  2. // push可接收两个参数,第一个参数path可以是字符串,或者对象,第二个参数是state对象
  3. // 里面是可以被浏览器缓存的数据,当path是一个对象并且path中的state存在,同时也传递了
  4. // 第二个参数state,那么这里就会给出警告,表示path中的state参数将会被忽略
  5. warning(
  6. !(typeof path === "object" && path.state !== undefined && state !== undefined),
  7. "You should avoid providing a 2nd state argument to push when the 1st " +
  8. "argument is a location-like object that already has state; it is ignored"
  9. )
  10. const action = "PUSH" // 动作为push操作
  11. //将即将访问的路径path, 被缓存的state,将要访问的路径的随机生成的6位随机字符串,
  12. // 上次访问过的location对象也可以理解为当前地址栏里路径对象,
  13. // 返回一个对象包含 pathname,search,hash,state,key
  14. const location = createLocation(path, state, createKey(), history.location)
  15. // 路由的切换,最后一个参数为回调函数,只有返回true的时候才会进行路由的切换
  16. transitionManager.confirmTransitionTo(location, action, getUserConfirmation, (ok) => {
  17. if (!ok)
  18. return
  19. const href = createHref(location) // 拼接basename
  20. const { key, state } = location // 获取新的key和state
  21. if (canUseHistory) {
  22. // 当可以使用history api时候,调用原生的pushState方法更改地址栏路径
  23. // 此时只是改变地址栏路径 页面并不会发生变化 需要手动setState从而rerender
  24. // pushState的三个参数分别为,1.可以被缓存的state对象,即刷新浏览器依然会保留
  25. // 2.页面的title,可直接忽略 3.href即新的地址栏路径,这是一个完整的路径地址
  26. globalHistory.pushState({ key, state }, null, href)
  27. if (forceRefresh) {
  28. window.location.href = href // 强制刷新
  29. } else {
  30. // 获取上次访问的路径的key在记录列表里的下标
  31. const prevIndex = allKeys.indexOf(history.location.key)
  32. // 当下标存在时,返回截取到当前下标的数组key列表的一个新引用,不存在则返回一个新的空数组
  33. // 这样做的原因是什么?为什么不每次访问直接向allKeys列表中直接push要访问的key
  34. // 比如这样的一种场景, 1-2-3-4 的页面访问顺序,这时候使用go(-2) 回退到2的页面,假如在2
  35. // 的页面我们选择了push进行跳转到4页面,如果只是简单的对allKeys进行push操作那么顺序就变成了
  36. // 1-2-3-4-4,这时候就会产生一悖论,从4页面跳转4页面,这种逻辑是不通的,所以每当push或者replace
  37. // 发生的时候,一定是用当前地址栏中path的key去截取allKeys中对应的访问记录,来保证不会push
  38. // 连续相同的页面
  39. const nextKeys = allKeys.slice(0, prevIndex === -1 ? 0 : prevIndex + 1)
  40. nextKeys.push(location.key) // 将新的key添加到allKeys中
  41. allKeys = nextKeys // 替换
  42. setState({ action, location }) // render页面
  43. }
  44. } else {
  45. warning(
  46. state === undefined,
  47. "Browser history cannot push state in browsers that do not support HTML5 history"
  48. )
  49. window.location.href = href
  50. }
  51. })
  52. }

createLocation的源码

</>复制代码

  1. export const createLocation = (path, state, key, currentLocation) => {
  2. let location
  3. if (typeof path === "string") {
  4. // Two-arg form: push(path, state)
  5. // 分解pathname,path,hash,search等,parsePath返回一个对象
  6. location = parsePath(path)
  7. location.state = state
  8. } else {
  9. // One-arg form: push(location)
  10. location = { ...path }
  11. if (location.pathname === undefined)
  12. location.pathname = ""
  13. if (location.search) {
  14. if (location.search.charAt(0) !== "?")
  15. location.search = "?" + location.search
  16. } else {
  17. location.search = ""
  18. }
  19. if (location.hash) {
  20. if (location.hash.charAt(0) !== "#")
  21. location.hash = "#" + location.hash
  22. } else {
  23. location.hash = ""
  24. }
  25. if (state !== undefined && location.state === undefined)
  26. location.state = state
  27. }
  28. // 尝试对pathname进行decodeURI解码操作,失败时进行提示
  29. try {
  30. location.pathname = decodeURI(location.pathname)
  31. } catch (e) {
  32. if (e instanceof URIError) {
  33. throw new URIError(
  34. "Pathname "" + location.pathname + "" could not be decoded. " +
  35. "This is likely caused by an invalid percent-encoding."
  36. )
  37. } else {
  38. throw e
  39. }
  40. }
  41. if (key)
  42. location.key = key
  43. if (currentLocation) {
  44. // Resolve incomplete/relative pathname relative to current location.
  45. if (!location.pathname) {
  46. location.pathname = currentLocation.pathname
  47. } else if (location.pathname.charAt(0) !== "/") {
  48. location.pathname = resolvePathname(location.pathname, currentLocation.pathname)
  49. }
  50. } else {
  51. // When there is no prior location and pathname is empty, set it to /
  52. // pathname 不存在的时候返回当前路径的根节点
  53. if (!location.pathname) {
  54. location.pathname = "/"
  55. }
  56. }
  57. // 返回一个location对象包含
  58. // pathname,search,hash,state,key
  59. return location
  60. }

createTransitionManager.js的源码

</>复制代码

  1. import warning from "warning"
  2. const createTransitionManager = () => {
  3. // 这里使一个闭包环境,每次进行路由切换的时候,都会先进行对prompt的判断
  4. // 当prompt != null 的时候,表示路由的上次切换被阻止了,那么当用户confirm返回true
  5. // 的时候会直接进行地址栏的更新和subscribe的回调
  6. let prompt = null // 提示符
  7. const setPrompt = (nextPrompt) => {
  8. // 提示prompt只能存在一个
  9. warning(
  10. prompt == null,
  11. "A history supports only one prompt at a time"
  12. )
  13. prompt = nextPrompt
  14. // 同时将解除block的方法返回
  15. return () => {
  16. if (prompt === nextPrompt)
  17. prompt = null
  18. }
  19. }
  20. //
  21. const confirmTransitionTo = (location, action, getUserConfirmation, callback) => {
  22. // TODO: If another transition starts while we"re still confirming
  23. // the previous one, we may end up in a weird state. Figure out the
  24. // best way to handle this.
  25. if (prompt != null) {
  26. // prompt 可以是一个函数,如果是一个函数返回执行的结果
  27. const result = typeof prompt === "function" ? prompt(location, action) : prompt
  28. // 当prompt为string类型时 基本上就是为了提示用户即将要跳转路由了,prompt就是提示信息
  29. if (typeof result === "string") {
  30. // 调用window.confirm来显示提示信息
  31. if (typeof getUserConfirmation === "function") {
  32. // callback接收用户 选择了true或者false
  33. getUserConfirmation(result, callback)
  34. } else {
  35. // 提示开发者 getUserConfirmatio应该是一个function来展示阻止路由跳转的提示
  36. warning(
  37. false,
  38. "A history needs a getUserConfirmation function in order to use a prompt message"
  39. )
  40. // 相当于用户选择true 不进行拦截
  41. callback(true)
  42. }
  43. } else {
  44. // Return false from a transition hook to cancel the transition.
  45. callback(result !== false)
  46. }
  47. } else {
  48. // 当不存在prompt时,直接执行回调函数,进行路由的切换和rerender
  49. callback(true)
  50. }
  51. }
  52. // 被subscribe的列表,即在Router组件添加的setState方法,每次push replace 或者 go等操作都会触发
  53. let listeners = []
  54. // 将回调函数添加到listeners,一个发布订阅模式
  55. const appendListener = (fn) => {
  56. let isActive = true
  57. // 这里有个奇怪的地方既然订阅事件可以被解绑就直接被从数组中删除掉了,为什么这里还需要这个isActive
  58. // 再加一次判断呢,其实是为了避免一种情况,比如注册了多个listeners: a,b,c 但是在a函数中注销了b函数
  59. // 理论上来说b函数应该不能在执行了,但是注销方法里使用的是数组的filter,每次返回的是一个新的listeners引用,
  60. // 故每次解绑如果不添加isActive这个开关,那么当前循环还是会执行b的事件。加上isActive后,原始的liteners中
  61. // 的闭包b函数的isActive会变为false,从而阻止事件的执行,当循环结束后,原始的listeners也会被gc回收
  62. const listener = (...args) => {
  63. if (isActive)
  64. fn(...args)
  65. }
  66. listeners.push(listener)
  67. return () => {
  68. isActive = false
  69. listeners = listeners.filter(item => item !== listener)
  70. }
  71. }
  72. // 通知被订阅的事件开始执行
  73. const notifyListeners = (...args) => {
  74. listeners.forEach(listener => listener(...args))
  75. }
  76. return {
  77. setPrompt,
  78. confirmTransitionTo,
  79. appendListener,
  80. notifyListeners
  81. }
  82. }
  83. export default createTransitionManager

由于篇幅太长,自己都看的蒙圈了,现在就简单做一下总结,描述router工作的原理。
1.首先BrowserRouter通过history库使用createBrowserHistory方法创建了一个history对象,并将此对象作为props传递给了Router组件
2.Router组件使用history对的的listen方法,注册了组件自身的setState事件,这样一样来,只要触发了html5的popstate事件,组件就会执行setState事件,完成整个应用的rerender
3.history是一个对象,里面包含了操作页面跳转的方法,以及当前地址栏对象的location的信息。首先当创建一个history对象时候,会使用props当中的四个参数信息,forceRefresh、basename、getUserComfirmation、keyLength 来生成一个初始化的history对象,四个参数均不是必传项。首先会使用window.location对象获取当前路径下的pathname、search、hash等参数,同时如果页面是经过rerolad刷新过的页面,那么也会保存之前向state添加过数据,这里除了我们自己添加的state,还有history这个库自己每次做push或者repalce操作的时候随机生成的六位长度的字符串key
拿到这个初始化的location对象后,history开始封装push、replace、go等这些api。
以push为例,可以接收两个参数push(path, state)----我们常用的写法是push("/user/list"),只需要传递一个路径不带参数,或者push({pathname: "/user", state: {id: "xxx"}, search: "?name=xxx", hash: "#list"})传递一个对象。任何对地址栏的更新都会经过confirmTransitionTo 这个方法进行验证,这个方法是为了支持prompt拦截器的功能。正常在拦截器关闭的情况下,每次调用push或者replace都会随机生成一个key,代表这个路径的唯一hash值,并将用户传递的state和key作为state,注意这部分state会被保存到 浏览器 中是一个长效的缓存,将拼接好的path作为传递给history的第三个参数,调用history.pushState(state, null, path),这样地址栏的地址就得到了更新。
地址栏地址得到更新后,页面在不使用foreceRefrsh的情况下是不会自动更新的。此时需要循环执行在创建history对象时,在内存中的一个listeners监听队列,即在步骤2中在Router组件内部注册的回调,来手动完成页面的setState,至此一个完整的更新流程就算走完了。
在history里有一个block的方法,这个方法的初衷是为了实现对路由跳转的拦截。我们知道浏览器的回退和前进操作按钮是无法进行拦截的,只能做hack,这也是history库的做法。抽离出了一个路径控制器,方法名称叫做createTransitionManager,可以理解为路由操作器。这个方法在内部维护了一个prompt的拦截器开关,每当这个开关打开的时候,所有的路由在跳转前都会被window.confirm所拦截。注意此拦截并非真正的拦截,虽然页面没有改变,但是地址栏的路径已经改变了。如果用户没有取消拦截,那么页面依然会停留在当前页面,这样和地址栏的路径就产生了悖论,所以需要将地址栏的路径再重置为当前页面真正渲染的页面。为了实现这一功能,不得不创建了一个用随机key值的来表示的访问过的路径表allKeys。每次页面被拦截后,都需要在allKeys的列表中找到当前路径下的key的下标,以及实际页面显示的location的key的下标,后者减前者的值就是页面要被回退或者前进的次数,调用go方法后会再次触发popstate事件,造成页面的rerender。
正式因为有了Prompt组件才会使history不得不增加了key列表,prompt开关,导致代码的复杂度成倍增加,同时很多开发者在开发中对此组件的滥用也导致了一些特殊的bug,并且这些bug都是无法解决的,这也是作者为什么想要在下个版本中移除此api的缘由。讨论地址在链接描述

。下篇将会进行对Route Switch Link等其他组件的讲解

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/109660.html

相关文章

  • react-router v4.x 源码拾遗1

    摘要:还是先来一段官方的基础使用案例,熟悉一下整体的代码流程中使用了端常用到的等一些常用组件,作为的顶层组件来获取的和设置回调函数来更新。 react-router是react官方推荐并参与维护的一个路由库,支持浏览器端、app端、服务端等常见场景下的路由切换功能,react-router本身不具备切换和跳转路由的功能,这些功能全部由react-router依赖的history库完成,his...

    Joyven 评论0 收藏0
  • react-router v4.x 源码拾遗2

    摘要:如果将添加到当前组件,并且当前组件由包裹,那么将引用最外层包装组件的实例而并非我们期望的当前组件,这也是在实际开发中为什么不推荐使用的原因,使用一个回调函数是一个不错的选择,也同样的使用的是回调函数来实现的。 回顾:上一篇讲了BrowserRouter 和 Router之前的关系,以及Router实现路由跳转切换的原理。这一篇来简短介绍react-router剩余组件的源码,结合官方文...

    luoyibu 评论0 收藏0
  • react-router v4.x 源码拾遗2

    摘要:如果将添加到当前组件,并且当前组件由包裹,那么将引用最外层包装组件的实例而并非我们期望的当前组件,这也是在实际开发中为什么不推荐使用的原因,使用一个回调函数是一个不错的选择,也同样的使用的是回调函数来实现的。 回顾:上一篇讲了BrowserRouter 和 Router之前的关系,以及Router实现路由跳转切换的原理。这一篇来简短介绍react-router剩余组件的源码,结合官方文...

    CoorChice 评论0 收藏0

发表评论

0条评论

itvincent

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<