最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

淺析history 和 react-router 的實現原理

 更新時間:2023年08月17日 09:47:15   作者:袋鼠云數棧前端  
react-router 版本更新非???但是它的底層實現原理確是萬變不離其中,在本文中會從前端路由出發(fā)到 react-router 原理總結與分享,本文對history 和 react-router實現原理講解的非常詳細,需要的朋友跟隨小編一起看看吧

前言

在前一篇文章中,我們詳細的說了 react-router@3.x 升級到 @6.x 需要注意的問題以及變更的使用方式。

react-router 版本更新非常快,但是它的底層實現原理確是萬變不離其中,在本文中會從前端路由出發(fā)到 react-router 原理總結與分享。

前端路由

在 Web 前端單頁面應用 SPA(Single Page Application)中,路由是描述 URL 和 UI 之間的映射關系,這種映射是單向的,即 URL 的改變會引起 UI 更新,無需刷新頁面

如何實現前端路由

實現前端路由,需要解決兩個核心問題

  • 如何改變 URL 卻不引起頁面刷新?
  • 如何監(jiān)測 URL 變化?

在前端路由的實現模式有兩種模式,hash 和 history 模式,分別回答上述兩個問題

hash 模式

  • hash 是 url 中 hash(#) 及后面的部分,常用錨點在頁面內做導航,改變 url 中的 hash 部分不會引起頁面的刷新
  • 通過 hashchange 事件監(jiān)聽 URL 的改變。改變 URL 的方式只有以下幾種:通過瀏覽器導航欄的前進后退、通過<a>標簽、通過window.location,這幾種方式都會觸發(fā)hashchange事件

history 模式

  • history 提供了 pushState 和 replaceState 兩個方法,這兩個方法改變 URL 的 path 部分不會引起頁面刷新
  • 通過 popchange 事件監(jiān)聽 URL 的改變。需要注意只在通過瀏覽器導航欄的前進后退改變 URL 時會觸發(fā)popstate事件,通過<a>標簽和pushState/replaceState不會觸發(fā)popstate方法。但我們可以攔截<a>標簽的點擊事件和pushState/replaceState的調用來檢測 URL 變化,也是可以達到監(jiān)聽 URL 的變化,相對hashchange顯得略微復雜

JS 實現前端路由

基于 hash 實現

由于三種改變 hash 的方式都會觸發(fā)hashchange方法,所以只需要監(jiān)聽hashchange方法。需要在DOMContentLoaded后,處理一下默認的 hash 值

// 頁面加載完不會觸發(fā) hashchange,這里主動觸發(fā)一次 hashchange 事件,處理默認hash
window.addEventListener('DOMContentLoaded', onLoad);
// 監(jiān)聽路由變化
window.addEventListener('hashchange', onHashChange);
// 路由變化時,根據路由渲染對應 UI
function onHashChange() {
  switch (location.hash) {
    case '#/home':
      routerView.innerHTML = 'This is Home';
      return;
    case '#/about':
      routerView.innerHTML = 'This is About';
      return;
    case '#/list':
      routerView.innerHTML = 'This is List';
      return;
    default:
      routerView.innerHTML = 'Not Found';
      return;
  }
}

hash 實現 demo

基于 history 實現

因為 history 模式下,<a>標簽和pushState/replaceState不會觸發(fā)popstate方法,我們需要對<a>的跳轉和pushState/replaceState做特殊處理。

  • <a>作點擊事件,禁用默認行為,調用pushState方法并手動觸發(fā)popstate的監(jiān)聽事件
  • pushState/replaceState可以重寫 history 的方法并通過派發(fā)事件能夠監(jiān)聽對應事件
var _wr = function (type) {
  var orig = history[type];
  return function () {
    var e = new Event(type);
    e.arguments = arguments;
    var rv = orig.apply(this, arguments);
    window.dispatchEvent(e);
    return rv;
  };
};
// 重寫pushstate事件
history.pushState = _wr('pushstate');
function onLoad() {
  routerView = document.querySelector('#routeView');
  onPopState();
  // 攔截 <a> 標簽點擊事件默認行為
  // 點擊時使用 pushState 修改 URL并更新手動 UI,從而實現點擊鏈接更新 URL 和 UI 的效果。
  var linkList = document.querySelectorAll('a[href]');
  linkList.forEach((el) =>
    el.addEventListener('click', function (e) {
      e.preventDefault();
      history.pushState(null, '', el.getAttribute('href'));
      onPopState();
    }),
  );
}
// 監(jiān)聽pushstate方法
window.addEventListener('pushstate', onPopState());
// 頁面加載完不會觸發(fā) hashchange,這里主動觸發(fā)一次 popstate 事件,處理默認pathname
window.addEventListener('DOMContentLoaded', onLoad);
// 監(jiān)聽路由變化
window.addEventListener('popstate', onPopState);
// 路由變化時,根據路由渲染對應 UI
function onPopState() {
  switch (location.pathname) {
    case '/home':
      routerView.innerHTML = 'This is Home';
      return;
    case '/about':
      routerView.innerHTML = 'This is About';
      return;
    case '/list':
      routerView.innerHTML = 'This is List';
      return;
    default:
      routerView.innerHTML = 'Not Found';
      return;
  }
}

history 實現 demo

React-Router 的架構

  • history 庫給 browser、hash 兩種 history 提供了統(tǒng)一的 API,給到 react-router-dom 使用
  • react-router 實現了路由的最核心能力。提供了<Router><Route>等組件,以及配套 hook
  • react-router-dom 是對 react-router 更上一層封裝。把 history 傳入<Router>并初始化成<BrowserRouter><HashRouter>,補充了<Link>這樣給瀏覽器直接用的組件。同時把 react-router 直接導出,減少依賴

History 實現

history

在上文中說到,BrowserRouter使用 history 庫提供的createBrowserHistory創(chuàng)建的history對象改變路由狀態(tài)和監(jiān)聽路由變化。

? 那么 history 對象需要提供哪些功能訥?

  • 監(jiān)聽路由變化的listen方法以及對應的清理監(jiān)聽unlisten方法
  • 改變路由的push方法
// 創(chuàng)建和管理listeners的方法
export const EventEmitter = () => {
  const events = [];
  return {
    subscribe(fn) {
      events.push(fn);
      return function () {
        events = events.filter((handler) => handler !== fn);
      };
    },
    emit(arg) {
      events.forEach((fn) => fn && fn(arg));
    },
  };
};

BrowserHistory

const createBrowserHistory = () => {
  const EventBus = EventEmitter();
  // 初始化location
  let location = {
    pathname: '/',
  };
  // 路由變化時的回調
  const handlePop = function () {
    const currentLocation = {
      pathname: window.location.pathname,
    };
    EventBus.emit(currentLocation); // 路由變化時執(zhí)行回調
  };
  // 定義history.push方法
  const push = (path) => {
    const history = window.history;
    // 為了保持state棧的一致性
    history.pushState(null, '', path);
    // 由于push并不觸發(fā)popstate,我們需要手動調用回調函數
    location = { pathname: path };
    EventBus.emit(location);
  };
  const listen = (listener) => EventBus.subscribe(listener);
  // 處理瀏覽器的前進后退
  window.addEventListener('popstate', handlePop);
  // 返回history
  const history = {
    location,
    listen,
    push,
  };
  return history;
};

對于 BrowserHistory 來說,我們的處理需要增加一項,當我們觸發(fā) push 的時候,需要手動通知所有的監(jiān)聽者,因為 pushState 無法觸發(fā) popState 事件,因此需要手動觸發(fā)

HashHistory

const createHashHistory = () => {
  const EventBus = EventEmitter();
  let location = {
    pathname: '/',
  };
  // 路由變化時的回調
  const handlePop = function () {
    const currentLocation = {
      pathname: window.location.hash.slice(1),
    };
    EventBus.emit(currentLocation); // 路由變化時執(zhí)行回調
  };
  // 不用手動執(zhí)行回調,因為hash改變會觸發(fā)hashchange事件
  const push = (path) => (window.location.hash = path);
  const listen = (listener: Function) => EventBus.subscribe(listener);
  // 監(jiān)聽hashchange事件
  window.addEventListener('hashchange', handlePop);
  // 返回的history上有個listen方法
  const history = {
    location,
    listen,
    push,
  };
  return history;
};

在實現 hashHistory 的時候,我們只是對hashchange進行了監(jiān)聽,當該事件發(fā)生時,我們獲取到最新的 location 對象,在通知所有的監(jiān)聽者 listener 執(zhí)行回調函數

React-Router@6 丐版實現

  • 綠色為 history 中的方法
  • 紫色為 react-router-dom 中的方法
  • 橙色為 react-router 中的方法

Router

??? 基于 Context 的全局狀態(tài)下發(fā)。Router 是一個 “Provider-Consumer” 模型

Router 做的事情很簡單,接收navigator 和location,使用 context 將數據傳遞下去,能夠讓子組件獲取到相關的數據

function Router(props: IProps) {
  const { navigator, children, location } = props;
  const navigationContext = React.useMemo(() => ({ navigator }), [navigator]);
  const { pathname } = location;
  const locationContext = React.useMemo(
    () => ({ location: { pathname } }),
    [pathname],
  );
  return (
    <NavigationContext.Provider value={navigationContext}>
      <LocationContext.Provider value={locationContext} children={children} />
    </NavigationContext.Provider>
  );
}

HashRouter

基于不同的 history 調用 Router 組件。并且在 history 發(fā)生改變的時候,監(jiān)聽 history,能夠在 location 發(fā)生改變的時候,執(zhí)行回調改變 location。

在下面的代碼中,能夠發(fā)現監(jiān)聽者為 setState 函數,在上述 hashHistory 中,如果我們的 location 發(fā)生了改變,會通知到所有的監(jiān)聽者執(zhí)行回調,也就是我們這里的 setState 函數,即我們能夠拿到最新的 location 信息通過 LocationContext 傳遞給子組件,再去做對應的路由匹配

function HashRouter({ children }) {
  let historyRef = React.useRef();
  if (historyRef.current == null) {
    historyRef.current = createHashHistory();
  }
  let history = historyRef.current;
  let [state, setState] = React.useState({
    location: history.location,
  });
  React.useEffect(() => {
    const unListen = history.listen(setState);
    return unListen;
  }, [history]);
  return (
    <Router children={children} location={state.location} navigator={history} />
  );
}

Routes/Route

我們能夠發(fā)現在 v6.0 的版本 Route 組件只是一個工具人,并沒有做任何事情。

function Route(_props: RouteProps): React.ReactElement | null {
  invariant(
    false,
    `A <Route> is only ever to be used as the child of <Routes> element, ` +
      `never rendered directly. Please wrap your <Route> in a <Routes>.`,
  );
}

實際上處理一切邏輯的組件是 Routes,它內部實現了根據路由的變化,匹配出一個正確的組件。

const Routes = ({ children }) => {
  return useRoutes(createRoutesFromChildren(children));
};

useRoutes 為整個 v6 版本的核心,分為路由上下文解析、路由匹配、路由渲染三個步驟

<Routes>
  <Route path="/home" element={<Home />}>
    <Route path="1" element={<Home1 />}>
      <Route path="2" element={<Home2 />}></Route>
    </Route>
  </Route>
  <Route path="/about" element={<About />}></Route>
  <Route path="/list" element={<List />}></Route>
  <Route path="/notFound" element={<NotFound />} />
  <Route path="/navigate" element={<Navigate to="/notFound" />} />
</Routes>

上述 Routes 代碼中,通過 createRoutesFromChildren 函數將 Route 組件結構化??梢园?nbsp;<Route> 類型的 react element 對象,變成了普通的 route 對象結構,如下圖

useRoutes

useRoutes 才是真正處理渲染關系的,其代碼如下:

// 第一步:獲取相關的 pathname
let location = useLocation();
let { matches: parentMatches } = React.useContext(RouteContext);
// 第二步:找到匹配的路由分支,將 pathname 和 Route 的 path 做匹配
const matches = matchRoutes(routes, location);
// 第三步:渲染真正的路由組件
const renderedMatches = _renderMatches(matches, parentMatches);
return renderedMatches;

matchRoutes

matchRoutes 中通過 pathname 和路由的 path 進行匹配

因為我們在 Route 中定義的 path 都是相對路徑,所以我們在 matchRoutes 方法中,需要對 routes 對象遍歷,對于 children 里面的 path 需要變成完整的路徑,并且需要將 routes 扁平化,不在使用嵌套結構

const flattenRoutes = (
  routes,
  branches = [],
  parentsMeta = [],
  parentPath = '',
) => {
  const flattenRoute = (route) => {
    const meta = {
      relativePath: route.path || '',
      route,
    };
    const path = joinPaths([parentPath, meta.relativePath]);
    const routesMeta = parentsMeta.concat(meta);
    if (route.children?.length > 0) {
      flattenRoutes(route.children, branches, routesMeta, path);
    }
    if (route.path == null) {
      return;
    }
    branches.push({ path, routesMeta });
  };
  routes.forEach((route) => {
    flattenRoute(route);
  });
  return branches;
};

當我們訪問/#/home/1/2的時候,獲得的 matches 如下

我們得到的 match 順序是從 Home → Home1 → Home2

_renderMatches

_renderMatches 才會渲染所有的 matches 對象

const _renderMatches = (matches, parentMatches = []) => {
  let renderedMatches = matches;
  return renderedMatches.reduceRight((outlet, match, index) => {
    let matches = parentMatches.concat(renderedMatches.slice(0, index + 1));
    const getChildren = () => {
      let children;
      if (match.route.Component) {
        children = <match.route.Component />;
      } else if (match.route.element) {
        children = match.route.element;
      } else {
        children = outlet;
      }
      return (
        <RouteContext.Provider
          value={{
            outlet,
            matches,
          }}
        >
          {children}
        </RouteContext.Provider>
      );
    };
    return getChildren();
  }, null);
};

_renderMatches 這段代碼我們能夠明白 outlet 作為子路由是如何傳遞給父路由渲染的。matches 采用從右往左的遍歷順序,將上一項的返回值作為后一項的 outlet,那么子路由就作為 outlet 傳遞給了父路由

Outlet

實際上就是內部渲染 RouteContext 的 outlet 屬性

function Outlet(props) {
  return useOutlet(props.context);
}
function useOutlet(context?: unknown) {
  let outlet = useContext(RouteContext).outlet; // 獲取上一級 RouteContext 上面的 outlet
  if (outlet) {
    return (
      <OutletContext.Provider value={context}>{outlet}</OutletContext.Provider>
    );
  }
  return outlet;
}

Link

在 Link 中,我們使用<a>標簽來做跳轉,但是 a 標簽會使頁面重新刷新,所以需要阻止 a 標簽的默認行為,調用 useNavigate 方法進行跳轉

function Link({ to, children, onClick }) {
  const navigate = useNavigate();
  const handleClick = onClick
    ? onClick
    : (event) => {
        event.preventDefault();
        navigate(to);
      };
  return (
    <a href={to} onClick={handleClick}>
      {children}
    </a>
  );
}

Hooks

function useLocation() {
  return useContext(LocationContext).location;
}
function useNavigate() {
  const { navigator } = useContext(NavigationContext);
  const navigate = useCallback(
    (to: string) => {
      navigator.push(to);
    },
    [navigator],
  );
  return navigate;
}

本文所有的代碼鏈接可點擊查看

參考鏈接

到此這篇關于一文了解 history 和 react-router 的實現原理的文章就介紹到這了,更多相關history 和 react-router實現原理內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 深入理解React中Suspense與lazy的原理

    深入理解React中Suspense與lazy的原理

    在react中為我們提供了一個非常有用的組件,那就是Suspense,本文主要介紹了如何使用Suspense?和?react提供的lazy結合起來達到異步加載狀態(tài)的目的,感興趣的可以了解下
    2024-04-04
  • 淺談對于react-thunk中間件的簡單理解

    淺談對于react-thunk中間件的簡單理解

    這篇文章主要介紹了淺談對于react-thunk中間件的簡單理解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-05-05
  • React使用ref方法與場景介紹

    React使用ref方法與場景介紹

    這篇文章主要介紹了React使用ref方法與場景,React支持給任意組件添加特殊屬性。ref屬性接受一個回調函數,它在組件被加載或卸載時會立即執(zhí)行
    2022-10-10
  • Taro?React自定義TabBar使用useContext解決底部選中異常

    Taro?React自定義TabBar使用useContext解決底部選中異常

    這篇文章主要為大家介紹了Taro?React底部自定義TabBar使用React?useContext解決底部選中異常(需要點兩次才能選中的問題)示例詳解,有需要的朋友可以借鑒參考下
    2023-08-08
  • react-native彈窗封裝的方法

    react-native彈窗封裝的方法

    這篇文章主要為大家詳細介紹了react-native彈窗封裝的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • React?中使用?Redux?的?4?種寫法小結

    React?中使用?Redux?的?4?種寫法小結

    這篇文章主要介紹了在?React?中使用?Redux?的?4?種寫法,Redux 一般來說并不是必須的,只有在項目比較復雜的時候,比如多個分散在不同地方的組件使用同一個狀態(tài),本文就React使用?Redux的相關知識給大家介紹的非常詳細,需要的朋友參考下吧
    2022-06-06
  • 一文帶你了解React中的函數組件

    一文帶你了解React中的函數組件

    函數式組件的基本意義就是,組件實際上是一個函數,不是類,下面這篇文章主要給大家介紹了關于React中函數組件的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-08-08
  • React Agent 自定義實現代碼

    React Agent 自定義實現代碼

    在使用langchain的ReactAgent遇到問題后,作者嘗試自定義ReactAgent實現,通過詳細分析langchain中的agent功能和問題,結合React思想,作者設計了新的agent邏輯并在GitHub上分享了代碼,新的ReactAgent通過改進prompt和工具調用邏輯,提升了任務執(zhí)行的效果和穩(wěn)定性
    2024-10-10
  • Hello?React的組件化方式之React入門小案例演示

    Hello?React的組件化方式之React入門小案例演示

    這篇文章主要介紹了Hello?React的組件化方式-React入門小案例,本文通過Hello?React的案例,?來體驗一下React開發(fā)模式,?以及jsx的語法,需要的朋友可以參考下
    2022-10-10
  • 教你react中如何理解usestate、useEffect副作用、useRef標識和useContext

    教你react中如何理解usestate、useEffect副作用、useRef標識和useContext

    這篇文章主要介紹了react中如何理解usestate、useEffect副作用、useRef標識和useContext,其實與vue中的ref和reactive一樣,通過useState獲取到的數據可以實現組件視圖實時交互,而普通定義的數據僅僅在業(yè)務中使用,需要的朋友可以參考下
    2022-11-11

最新評論

绥滨县| 阜新市| 榆中县| 涡阳县| 湖北省| 宝应县| 政和县| 麦盖提县| 长垣县| 龙口市| 辉县市| 苏尼特左旗| 额尔古纳市| 德州市| 山东省| 锡林郭勒盟| 太和县| 铜陵市| 海晏县| 济南市| 竹北市| 福泉市| 德清县| 龙山县| 昭苏县| 池州市| 永德县| 清水河县| 邛崃市| 布尔津县| 伊川县| 大邑县| 靖边县| 额敏县| 邯郸县| 青田县| 资阳市| 谢通门县| 额济纳旗| 项城市| 尉氏县|