动态

详情 返回 返回

一文了解 history 和 react-router 的實現原理 - 动态 详情

我們是袋鼠雲數棧 UED 團隊,致力於打造優秀的一站式數據中台產品。我們始終保持工匠精神,探索前端道路,為社區積累並傳播經驗價值。

本文作者:霜序

前言

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

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

前端路由

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

如何實現前端路由

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

  1. 如何改變 URL 卻不引起頁面刷新?
  2. 如何監測 URL 變化?

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

hash 模式

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

history 模式

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

JS 實現前端路由

基於 hash 實現

由於三種改變 hash 的方式都會觸發hashchange方法,所以只需要監聽hashchange方法。需要在DOMContentLoaded後,處理一下默認的 hash 值

// 頁面加載完不會觸發 hashchange,這裏主動觸發一次 hashchange 事件,處理默認hash
window.addEventListener('DOMContentLoaded', onLoad);
// 監聽路由變化
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不會觸發popstate方法,我們需要對<a>的跳轉和pushState/replaceState做特殊處理。

  • <a>作點擊事件,禁用默認行為,調用pushState方法並手動觸發popstate的監聽事件
  • pushState/replaceState可以重寫 history 的方法並通過派發事件能夠監聽對應事件
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();
    }),
  );
}
// 監聽pushstate方法
window.addEventListener('pushstate', onPopState());
// 頁面加載完不會觸發 hashchange,這裏主動觸發一次 popstate 事件,處理默認pathname
window.addEventListener('DOMContentLoaded', onLoad);
// 監聽路由變化
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 的架構

file

  • history 庫給 browser、hash 兩種 history 提供了統一的 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創建的history對象改變路由狀態和監聽路由變化。

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

  • 監聽路由變化的listen方法以及對應的清理監聽unlisten方法
  • 改變路由的push方法
// 創建和管理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); // 路由變化時執行回調
  };
  // 定義history.push方法
  const push = (path) => {
    const history = window.history;
    // 為了保持state棧的一致性
    history.pushState(null, '', path);
    // 由於push並不觸發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 來説,我們的處理需要增加一項,當我們觸發 push 的時候,需要手動通知所有的監聽者,因為 pushState 無法觸發 popState 事件,因此需要手動觸發

HashHistory

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

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

React-Router@6 丐版實現

file

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

Router

🎗️ 基於 Context 的全局狀態下發。Router 是一個 “Provider-Consumer” 模型

Router 做的事情很簡單,接收navigatorlocation,使用 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 發生改變的時候,監聽 history,能夠在 location 發生改變的時候,執行回調改變 location。

在下面的代碼中,能夠發現監聽者為 setState 函數,在上述 hashHistory 中,如果我們的 location 發生了改變,會通知到所有的監聽者執行回調,也就是我們這裏的 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

我們能夠發現在 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 組件結構化。可以把 <Route> 類型的 react element 對象,變成了普通的 route 對象結構,如下圖

file

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;
};

file

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

file

我們得到的 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 傳遞給了父路由

file

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;
}

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

參考鏈接
  • react router v6 使用詳解以及部分源碼解析(新老版本對比) - 掘金
  • 「React 進階」react-router v6 通關指南 - 掘金
  • 一文讀懂 react-router 原理

最後

歡迎關注【袋鼠雲數棧UED團隊】~
袋鼠雲數棧UED團隊持續為廣大開發者分享技術成果,相繼參與開源了歡迎star

  • 大數據分佈式任務調度系統——Taier
  • 輕量級的 Web IDE UI 框架——Molecule
  • 針對大數據領域的 SQL Parser 項目——dt-sql-parser
  • 袋鼠雲數棧前端團隊代碼評審工程實踐文檔——code-review-practices
  • 一個速度更快、配置更靈活、使用更簡單的模塊打包器——ko
user avatar cyzf 头像 Leesz 头像 haoqidewukong 头像 kobe_fans_zxc 头像 dirackeeko 头像 razyliang 头像 dunizb 头像 febobo 头像 imba97 头像 Z-HarOld 头像 libubai 头像 kitty-38 头像
点赞 109 用户, 点赞了这篇动态!
点赞

Add a new 评论

Some HTML is okay.