1. <ins id="wdz05"><acronym id="wdz05"></acronym></ins>

          1. <menuitem id="wdz05"><video id="wdz05"></video></menuitem>
          2. <menuitem id="wdz05"></menuitem>
            <output id="wdz05"><track id="wdz05"></track></output>

            1. <ins id="wdz05"><acronym id="wdz05"></acronym></ins>
              溫馨提示×

              React高階組件如何創建一個面包屑導航

              發布時間:2022-10-24 17:49:53 來源:億速云 閱讀:91 作者:iii 欄目:web開發

              這篇文章主要介紹“React高階組件如何創建一個面包屑導航”,在日常操作中,相信很多人在React高階組件如何創建一個面包屑導航問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”React高階組件如何創建一個面包屑導航”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

              什么是 React 高階組件

              React 高階組件就是以高階函數的方式包裹需要修飾的 React 組件,并返回處理完成后的 React 組件。React 高階組件在 React 生態中使用的非常頻繁,比如react-router 中的 withRouter 以及 react-reduxconnect 等許多 API 都是以這樣的方式來實現的。

              使用 React 高階組件的好處

              在工作中,我們經常會有很多功能相似,組件代碼重復的頁面需求,通常我們可以通過完全復制一遍代碼的方式實現功能,但是這樣頁面的維護可維護性就會變得極差,需要對每一個頁面里的相同組件去做更改。因此,我們可以將其中共同的部分,比如接受相同的查詢操作結果、組件外同一的標簽包裹等抽離出來,做一個單獨的函數,并傳入不同的業務組件作為子組件參數,而這個函數不會修改子組件,只是通過組合的方式將子組件包裝在容器組件中,是一個無副作用的純函數,從而我們能夠在不改變這些組件邏輯的情況下將這部分代碼解耦,提升代碼可維護性。

              自己動手實現一個高階組件

              前端項目里,帶鏈接指向的面包屑導航十分常用,但由于面包屑導航需要手動維護一個所有目錄路徑與目錄名映射的數組,而這里所有的數據我們都能從 react-router 的路由表中取得,因此我們可以從這里入手,實現一個面包屑導航的高階組件。

              首先我們看看我們的路由表提供的數據以及目標面包屑組件所需要的數據:

              // 這里展示的是 react-router4 的route示例
              let routes = [
                {
                  breadcrumb: '一級目錄',
                  path: '/a',
                  component: require('../a/index.js').default,
                  items: [
                    {
                      breadcrumb: '二級目錄',
                      path: '/a/b',
                      component: require('../a/b/index.js').default,
                      items: [
                        {
                          breadcrumb: '三級目錄1',
                          path: '/a/b/c1',
                          component: require('../a/b/c1/index.js').default,
                          exact: true,
                        },
                        {
                          breadcrumb: '三級目錄2',
                          path: '/a/b/c2',
                          component: require('../a/b/c2/index.js').default,
                          exact: true,
                        },
                    }
                  ]
                }
              ]
              
              // 理想中的面包屑組件
              // 展示格式為 a / b / c1 并都附上鏈接
              const BreadcrumbsComponent = ({ breadcrumbs }) => (
                <div>
                  {breadcrumbs.map((breadcrumb, index) => (
                    <span key={breadcrumb.props.path}>
                      <link to={breadcrumb.props.path}>{breadcrumb}</link>
                      {index < breadcrumbs.length - 1 && <i> / </i>}
                    </span>
                  ))}
                </div>
              );

              這里我們可以看到,面包屑組件需要提供的數據一共有三種,一種是當前頁面的路徑,一種是面包屑所帶的文字,一種是該面包屑的導航鏈接指向。

              其中第一種我們可以通過 react-router 提供的 withRouter 高階組件包裹,可使子組件獲取到當前頁面的 location 屬性,從而獲取頁面路徑。

              后兩種需要我們對 routes 進行操作,首先將 routes 提供的數據扁平化成面包屑導航需要的格式,我們可以使用一個函數來實現它。

              /**
               * 以遞歸的方式展平react router數組
               */
              const flattenRoutes = arr =>
                arr.reduce(function(prev, item) {
                  prev.push(item);
                  return prev.concat(
                    Array.isArray(item.items) ? flattenRoutes(item.items) : item
                  );
                }, []);

              之后將展平的目錄路徑映射與當前頁面路徑一同放入處理函數,生成面包屑導航結構。

              export const getBreadcrumbs = ({ flattenRoutes, location }) => {
                // 初始化匹配數組match
                let matches = [];
              
                location.pathname
                  // 取得路徑名,然后將路徑分割成每一路由部分.
                  .split('?')[0]
                  .split('/')
                  // 對每一部分執行一次調用`getBreadcrumb()`的reduce.
                  .reduce((prev, curSection) => {
                    // 將最后一個路由部分與當前部分合并,比如當路徑為 `/x/xx/xxx` 時,pathSection分別檢查 `/x` `/x/xx` `/x/xx/xxx` 的匹配,并分別生成面包屑
                    const pathSection = `${prev}/${curSection}`;
                    const breadcrumb = getBreadcrumb({
                      flattenRoutes,
                      curSection,
                      pathSection,
                    });
              
                    // 將面包屑導入到matches數組中
                    matches.push(breadcrumb);
              
                    // 傳遞給下一次reduce的路徑部分
                    return pathSection;
                  });
                return matches;
              };

              然后對于每一個面包屑路徑部分,生成目錄名稱并附上指向對應路由位置的鏈接屬性。

              const getBreadcrumb = ({ flattenRoutes, curSection, pathSection }) => {
                const matchRoute = flattenRoutes.find(ele => {
                  const { breadcrumb, path } = ele;
                  if (!breadcrumb || !path) {
                    throw new Error(
                      'Router中的每一個route必須包含 `path` 以及 `breadcrumb` 屬性'
                    );
                  }
                  // 查找是否有匹配
                  // exact 為 react router4 的屬性,用于精確匹配路由
                  return matchPath(pathSection, { path, exact: true });
                });
              
                // 返回breadcrumb的值,沒有就返回原匹配子路徑名
                if (matchRoute) {
                  return render({
                    content: matchRoute.breadcrumb || curSection,
                    path: matchRoute.path,
                  });
                }
              
                // 對于routes表中不存在的路徑
                // 根目錄默認名稱為首頁.
                return render({
                  content: pathSection === '/' ? '首頁' : curSection,
                  path: pathSection,
                });
              };

              之后由 render 函數生成最后的單個面包屑導航樣式。單個面包屑組件需要為 render 函數提供該面包屑指向的路徑 path, 以及該面包屑內容映射content 這兩個 props。

              /**
               *
               */
              const render = ({ content, path }) => {
                const componentProps = { path };
                if (typeof content === 'function') {
                  return <content {...componentProps} />;
                }
                return <span {...componentProps}>{content}</span>;
              };

              有了這些功能函數,我們就能實現一個能為包裹組件傳入當前所在路徑以及路由屬性的 React 高階組件了。傳入一個組件,返回一個新的相同的組件結構,這樣便不會對組件外的任何功能與操作造成破壞。

              const BreadcrumbsHoc = (
                location = window.location,
                routes = []
              ) => Component => {
                const BreadComponent = (
                  <Component
                    breadcrumbs={getBreadcrumbs({
                      flattenRoutes: flattenRoutes(routes),
                      location,
                    })}
                  />
                );
                return BreadComponent;
              };
              export default BreadcrumbsHoc;

              調用這個高階組件的方法也非常簡單,只需要傳入當前所在路徑以及整個 react router 生成的 routes 屬性即可。
              至于如何取得當前所在路徑,我們可以利用 react router 提供的 withRouter 函數,如何使用請自行查閱相關文檔。
              值得一提的是,withRouter 本身就是一個高階組件,能為包裹組件提供包括 location 屬性在內的若干路由屬性。所以這個 API 也能作為學習高階組件一個很好的參考。

              withRouter(({ location }) =>
                BreadcrumbsHoc(location, routes)(BreadcrumbsComponent)
              );

              Q&A

              • 如果react router 生成的 routes 不是由自己手動維護的,甚至都沒有存在本地,而是通過請求拉取到的,存儲在 redux 里,通過 react-redux 提供的 connect 高階函數包裹時,路由發生變化時并不會導致該面包屑組件更新。使用方法如下:

              function mapStateToProps(state) {
                return {
                  routes: state.routes,
                };
              }
              
              connect(mapStateToProps)(
                withRouter(({ location }) =>
                  BreadcrumbsHoc(location, routes)(BreadcrumbsComponent)
                )
              );

              這其實是 connect 函數的一個bug。因為 react-redux 的 connect 高階組件會為傳入的參數組件實現 shouldComponentUpdate 這個鉤子函數,導致只有 prop 發生變化時才觸發更新相關的生命周期函數(含 render),而很顯然,我們的 location 對象并沒有作為 prop 傳入該參數組件。

              官方推薦的做法是使用 withRouter 來包裹 connectreturn value,即

              withRouter(
                connect(mapStateToProps)(({ location, routes }) =>
                  BreadcrumbsHoc(location, routes)(BreadcrumbsComponent)
                )
              );

              其實我們從這里也可以看出,高階組件同高階函數一樣,不會對組件的類型造成任何更改,因此高階組件就如同鏈式調用一樣,可以任意多層包裹來給組件傳入不同的屬性,在正常情況下也可以隨意調換位置,在使用上非常的靈活。這種可插拔特性使得高階組件非常受React生態的青睞,很多開源庫里都能看到這種特性的影子,有空也可以都拿出來分析一下。

              到此,關于“React高階組件如何創建一個面包屑導航”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!

              免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

              主題地圖

              成年人无码视频
              1. <ins id="wdz05"><acronym id="wdz05"></acronym></ins>

                    1. <menuitem id="wdz05"><video id="wdz05"></video></menuitem>
                    2. <menuitem id="wdz05"></menuitem>
                      <output id="wdz05"><track id="wdz05"></track></output>

                      1. <ins id="wdz05"><acronym id="wdz05"></acronym></ins>