您好,欢迎访问一九零五行业门户网

React Router中的核心history库的详细分析

这篇文章给大家介绍的内容是关于react router中的核心history库的详细分析,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
前言使用react开发稍微复杂一点的应用,react router几乎是路由管理的唯一选择。虽然react router经历了4个大版本的更新,功能也越来越丰富,但无论怎么变,它的核心依赖history库却一直没变。下面我们来了解下这个在github上有4k+星的库到底提供了什么功能。
html5 history对象聊到history库,是不是觉得这个单词有点熟悉?不错,html5规范里面,也新增了一个同名的history对象。下面我们来看下这个history对象用来解决什么问题。
在jquery统治前端的年代,通过ajax请求无刷新更新页面是当时相当流行的页面处理方式,spa的雏形就是那时候演化出来的。为了标示页面发生的变化,方便刷新后依然能显示正确的页面元素,一般会通过改变url的hash值来唯一定位页面。但这会带来另一个问题:用户无法使用前进/后退来切换页面。
为了解决这个问题,history对象应运而生。当页面的url或者hash发生变化的时候,浏览器会自动将新的url push到history对象中。history对象内部会维护一个state数组,记录url的变化。在浏览器进行前进/后退操作的时候,实际上就是调用history对象的对应方法(forward/back),取出对应的state,从而进行页面的切换。
除了操作url,history对象还提供2个不用通过操作url也能更新内部state的方法,分别是pushstate和replacestate。还能将额外的数据存到state中,然后在onpopstate事件中再通过event.state取出来。如果希望对history对象作更深入的理解,可以参考 这里,和这里。
history库与html5 history对象的关系我们再回过头来看history库。它本质上做了以下4件事情:
借鉴html5 history对象的理念,在其基础上又扩展了一些功能
提供3种类型的history:browserhistory,hashhistory,memoryhistory,并保持统一的api
支持发布/订阅功能,当history发生改变的时候,可以自动触发订阅的函数
提供跳转拦截、跳转确认和basename等实用功能
再对比一些两者api的异同。以下是history库的:
const history = {    length,        // 属性,history中记录的state的数量    action,        // 属性,当前导航的action类型    location,      // 属性,location对象,封装了pathname、search和hash等属性    push,          // 方法,导航到新的路由,并记录在history中    replace,       // 方法,替换掉当前记录在history中的路由信息    go,            // 方法,前进或后退n个记录    goback,        // 方法,后退    goforward,     // 方法,前进    cango,         // 方法,是否能前进或后退n个记录    block,         // 方法,跳转前让用户确定是否要跳转    listen         // 方法,订阅history变更事件  };
以下是html5 history对象的:
const history = {    length,         // 属性,history中记录的state的数量    state,          // 属性,pushstate和replacestate时传入的对象    back,           // 方法,后退    forward,        // 方法,前进    go,             // 方法,前进或后退n个记录    pushstate,      // 方法,导航到新的路由,并记录在history中    replacestate    // 方法,替换掉当前记录在history中的路由信息}// 订阅history变更事件window.onpopstate = function (event) {    ...}
从对比中可以看出,两者的关系是非常密切的,history库可以说是history对象的超集,是功能更强大的history对象。
createhashhistory源码分析下面,我们以三种history类型中的一种,hashhistory为例,来分析下history的源码,看看它都干了些什么。先看下它是怎么处理hash变更的。
// 构造hashhistory对象const createhashhistory = (props = {}) => {    ...    const globalhistory = window.history;    // 引用html5 history对象    ...    // transitionmanager负责控制是否进行跳转,以及跳转后要通知到的订阅者,后面会详细讨论    const transitionmanager = createtransitionmanager();    ...    // 注册history变更回调的订阅者    const listen = listener => {        const unlisten = transitionmanager.appendlistener(listener);        checkdomlisteners(1);        return () => {            checkdomlisteners(-1);            unlisten();        };    };        // 监听hashchange事件    const checkdomlisteners = delta => {        listenercount += delta;        if (listenercount === 1) {            window.addeventlistener(hashchangeevent, handlehashchange);        } else if (listenercount === 0) {            window.removeeventlistener(hashchangeevent, handlehashchange);        }    };        // hashchange事件回调    const handlehashchange = () => {        ...        // 构造内部使用的location对象,包含pathname、search和hash等属性        const location = getdomlocation();            ...        handlepop(location);    };        // 处理hash变更逻辑    const handlepop = location => {        ...        const action = pop;        // 给用户展示确认跳转的信息(如果有的话),确认后通知订阅者。如果用户取消跳转,则回退到之前状态        transitionmanager.confirmtransitionto(location, action, getuserconfirmation, ok => {            if (ok) {                setstate({action, location});    // 确认后通知订阅者            } else {                revertpop(location);             // 取消则回退到之前状态            }        });    };        // 更新action,location和length属性,并通知订阅者    const setstate = nextstate => {        object.assign(history, nextstate);        history.length = globalhistory.length;        transitionmanager.notifylisteners(history.location, history.action);    };    ...}
以上就是处理被动的hash变更的逻辑,一句话概括就是:订阅hash变更事件,判断是否确实要变更,如需变更则更新自己的属性,通知订阅者,不需变更则回退到之前的状态。
下面再看下transitionmanager做了什么,重点看发布/订阅相关内容,忽略用户确认跳转相关内容。
const createtransitionmanager = () => {    ...    // 内部维护的订阅者列表    let listeners = [];    // 注册订阅者    const appendlistener = fn => {        let isactive = true;        const listener = (...args) => {            if (isactive) fn(...args);        };        listeners.push(listener);        return () => {            isactive = false;            listeners = listeners.filter(item => item !== listener);        };    };    //通知订阅者    const notifylisteners = (...args) => {        listeners.foreach(listener => listener(...args));    };    ...}
这里的代码一目了然,就是维护一个订阅者列表,当hash变更的时候通知到相关的函数。
以上是hash改变的时候被动更新相关的内容,下面再看下主动更新相关的代码,以push为例,replace大同小异。
const push = (path, state) => {    ...    const action = push;    const location = createlocation(path, undefined, undefined, history.location);    transitionmanager.confirmtransitionto(location, action, getuserconfirmation, ok => {        if (!ok)     // 如果取消,则不跳转            return;        ...        pushhashpath(encodedpath);        // 用新的hash替换到url当中        ...        setstate({action, location});     // 更新action,location和length属性,并通知订阅者    });};// 用新的hash替换到url当中const pushhashpath = path => (window.location.hash = path);
在浏览器进行前进后退操作时,history库实际上是通过操作html5 history对象实现的。
const globalhistory = window.history;const go = n => {    ...    globalhistory.go(n);};const goback = () => go(-1);const goforward = () => go(1);
当调用window.history.go的时候,hash会发生变化,进而触发hashchange事件,然后history库再将变更通知到相关的订阅者。
总结本文对react router核心依赖history库进行了比较深入的介绍。从html5新增的history对象讲起,对比了它跟history库千丝万缕的关系,并以hashhistory为例子详细分析了其代码的实现细节。
最后,我们再来回顾一下history库做了哪些事情:
借鉴html5 history对象的理念,在其基础上又扩展了一些功能
提供3种类型的history:browserhistory,hashhistory,memoryhistory,并保持统一的api
支持发布/订阅功能,当history发生改变的时候,可以自动触发订阅的函数
提供跳转拦截、跳转确认和basename等实用功能
虽然history库是react router的核心依赖,但它跟react本身并没有依赖关系。如果你的项目中有操作history的场景,也可以将其引入到项目中来。
相关推荐:
使用h5实现react拖拽排序组件的方法(附代码)
html5如何解决margin-top的塌陷问题(附代码)
html5中标签和常用规则有哪些?html5标签以及规则的介绍
以上就是react router中的核心history库的详细分析的详细内容。
其它类似信息

推荐信息