React 中除了react-router還有哪些路由方案

在用React開發時,常用的路由是react-router ,但除此之外,還有兩個路由方案,因為他們具備 react-router 沒有的特性。

1.?@tanstack/router

1.1. 主要特性

  • 100% 推斷的 TypeScript 支持

  • 類型安全的導航

  • 嵌套路由和布局路由

  • 內置的路由加載器,帶有 SWR 緩存

  • 為客戶端數據緩存設計(如 TanStack Query、SWR 等)

  • 自動路由預取

  • 異步路由元素和錯誤邊界

  • 基于文件的路由生成

  • 類型安全的 JSON 優先搜索參數狀態管理 API

  • 路徑和搜索參數模式驗證

  • 搜索參數導航 API

  • 自定義搜索參數解析器/序列化器支持

  • 搜索參數中間件

  • 路由匹配/加載中間件

1.2. 基礎使用示例

import React, { StrictMode } from 'react'
import ReactDOM from 'react-dom/client'
import {Outlet,RouterProvider,Link,createRouter,createRoute,createRootRoute} from '@tanstack/react-router'import { TanStackRouterDevtools } from '@tanstack/router-devtools'const rootRoute = createRootRoute({component: () => (<><div className="p-2 flex gap-2"><Link to="/" className={[&.active]:font-bold">Home</Link>{' '}<Link to="/about" className={[&.active]:font-bold">About</Link></div><hr /><Outlet /><TanStackRouterDevtools /></>),
})const indexRoute = createRoute({getParentRoute: () => rootRoute,path: '/',component: function Index() {return (<div className="p-2"><h3>Welcome Home!</h3></div>)},
})const aboutRoute = createRoute({getParentRoute: () => rootRoute,path: '/about',component: function About() {return <div className="p-2">Hello from About!</div>},
})const routeTree = rootRoute.addChildren([indexRoute, aboutRoute])
const router = createRouter({ routeTree })declare module '@tanstack/react-router' {interface Register {router: typeof router}
}const rootElement = document.getElementById('app')!
if (!rootElement.innerHTML) {const root = ReactDOM.createRoot(rootElement)root.render(<StrictMode><RouterProvider router={router} /></StrictMode>,)
}

1.3.?原理淺析

@tanstack/router 的實現原理與 react-router 類似,關注的概念也完全相同,可以理解為它站在 react-router 的肩膀上,在某些細節上做了增強。

歷史記錄的實現:https://github.com/TanStack/router/blob/main/packages/history/src/index.ts

Router 入口:https://github.com/TanStack/router/blob/main/packages/react-router/src/link.tsx

2.?wouter

2.1. 主要特性

  • 最小依賴,壓縮后僅 2.1 KB,對比 React Router 的 18.7 KB。

  • 同時支持 React 和 Preact。

  • 沒有頂級的 <Router /> 組件,它是完全可選的。

  • 模仿 React Router 的最佳實踐,提供熟悉的 Route、Link、Switch 和 Redirect 組件。

  • 擁有基于 hook 的 API,用于更細粒度地控制路由:useLocation、useRoute 和 useRouter。

2.2. 基礎使用示例

import { Link, Route, Switch } from "wouter";const App = () => (<><Link href="/users/1">Profile</Link><Route path="/about">About Us</Route>{/* Routes below are matched exclusively - the first matched route gets rendered*/}<Switch><Route path="/inbox" component={InboxPage} /><Route path="/users/:name">{(params) => <>Hello, {params.name}!</>}</Route>{/* Default route in a switch */}<Route>404: No such page!</Route></Switch></>
);

2.3.?原理淺析

對于不同路由歷史與定位信息的封裝。

  • https://github.com/molefrog/wouter/blob/v3/packages/wouter/src/use-browser-location.js

  • https://github.com/molefrog/wouter/blob/v3/packages/wouter/src/use-hash-location.js

  • https://github.com/molefrog/wouter/blob/v3/packages/wouter/src/memory-location.js

import { parse as parsePattern } from "regexparam";
import {useBrowserLocation,useSearch as useBrowserSearch,
} from "./use-browser-location.js";
import {useRef,useContext,createContext,isValidateElement,cloneElement,createElement as h,Fragment,forwardRef,useIsomorphicLayoutEffect,useEvent,
} from "./react-deps.js";
import { absolutePath, relativePath, unescape, stripQm } from "./paths.js";// 定義默認的 Router 對象
const defaultRouter = {hook: useBrowserLocation,searchHook: useBrowserSearch,parser: parsePattern,base: "",ssrPath: undefined,ssrSearch: undefined,hrefs: (x) => x,
};// 創建一個 Router 上下文,提供給應用中的其他部分使用
const RouterCtx = createContext(defaultRouter);// 獲取最近的父級 router
export const useRouter = () => useContext(RouterCtx);// 創建一個參數上下文,提供給 `useParams()` 使用,以獲取匹配的參數
const ParamsCtx = createContext({});
export const useParams = () => useContext(ParamsCtx);// 內部版本的 useLocation 函數,避免多余的 useRouter 調用
const useLocationFromRouter = (router) => {const [location, navigate] = router.hook(router);return [unescape(relativePath(router.base, location)),useEvent((to, navOpts) => navigate(absolutePath(to, router.base), navOpts)),];
};// 使用 useRouter 獲取當前的 location
export const useLocation = () => useLocationFromRouter(useRouter());// 獲取當前搜索參數并返回
export const useSearch = () => {const router = useRouter();return unescape(stripQm(router.searchHook(router)));
};// 路由匹配函數
export const matchRoute = (parser, route, path, loose) => {const { pattern, keys } =route instanceof RegExp? { keys: false, pattern: route }: parser(route || "*", loose);const result = pattern.exec(path) || [];const [base, ...matches] = result;return base !== undefined? [true,() => {const groups =keys !== false? Object.fromEntries(keys.map((key, i) => [key, matches[i]])): result.groups;let obj = { ...matches };groups && object.assign(obj, groups);return obj;},...(loose ? [$base] : []),]: [false, null];
};// 使用 useRouter 獲取路由,并匹配路徑
export const useRoute = (pattern) =>matchRoute(useRouter().parser, pattern, useLocation()[0]);// Router 組件,用于提供自定義路由上下文
export const Router = ({ children, ...props }) => {const parent_ = useRouter();const parent = props.hook ? defaultRouter : parent_;let value;const [path, search] = props.ssrPath?.split("?") ?? [];if (search) (props.ssrSearch = search), (props.ssrPath = path);props.hrefs = props.hrefs ?? props.hook?.hrefs;let ref = useRef({}),prev = ref.current,next = prev;for (let k in parent) {const option =k === "base"? parent[k] + (props[k] || ""): props[k] || parent[k];if (prev === next && option !== next[k]) {ref.current = next = { ...next };}next[k] = option;if (option !== parent[k]) value = next;}return h(RouterCtx.Provider, { value, children });
};// 渲染 Route 組件,根據 props 提供不同渲染邏輯
const h_route = ({ children, component }, params) => {if (component) return h(component, { params });return typeof children === "function" ? children(params) : children;
};// Route 組件,用于匹配路徑并渲染對應組件
export const Route = ({ path, nest, match, ...renderProps }) => {const router = useRouter();const [location] = useLocationFromRouter(router);const [matches, params, base] =match ?? matchRoute(router.parser, path, location, nest);if (!matches) return null;const children = base? h(Router, { base }, h_route(renderProps, params)): h_route(renderProps, params);return h(ParamsCtx.Provider, { value: params, children });
};

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/pingmian/84954.shtml
繁體地址,請注明出處:http://hk.pswp.cn/pingmian/84954.shtml
英文地址,請注明出處:http://en.pswp.cn/pingmian/84954.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

VINS-Fusion 簡介、安裝、編譯、數據集/相機實測

目錄 VINS-Fusion 簡介 安裝 VINS-Fusion 源碼安裝 運行數據集 雙目模式 單目IMU 模式 雙目IMU 模式 D455 相機實際運行 雙目IMU 模式 VINS-Fusion 簡介 VINS-Fusion 是繼 VINS-Mono 和 VINS-Mobile&#xff08;單目視覺慣導 SLAM 方案&#xff09;后&#xff0c;香港科 技大學…

SQL Developer 表復制

SQL Developer 表復制 此方法在數據量比較大時&#xff0c;比一條一條的insert要快得多&#xff1b;具體是會覆蓋掉原數據&#xff0c;還是增量的處理&#xff0c;請自行創建demo表測試一下。 注意&#xff1a;原庫版本要與目標庫數據庫版本一致&#xff0c;否則可能會報錯的。…

影視劇學經典系列-梁祝-《呂氏春秋·應同》

1、背景 07版電視劇《梁山伯與祝英臺》中&#xff0c;謝道韞作為先生&#xff0c;給學生講了其中的句子。 2、名言 君為尊&#xff0c;以白為黑&#xff0c;臣不能從&#xff1b;父雖親&#xff0c;以黑為白&#xff0c;子不能從”出自《呂氏春秋應同》 其意為&#xff0c;…

異步爬蟲---

代碼結構分析 這是一個同步新聞爬蟲程序&#xff0c;主要包含以下幾個部分&#xff1a; 們把爬蟲設計為一個類&#xff0c;類在初始化時&#xff0c;連接數據庫&#xff0c;初始化logger&#xff0c;創建網址池&#xff0c;加載hubs并設置到網址池。 爬蟲開始運行的入口就是r…

微服務架構中的 Kafka:異步通信與服務解耦(二)

三、Kafka 基礎入門 3.1 Kafka 是什么 Kafka 最初由 LinkedIn 公司開發&#xff0c;是一個開源的分布式事件流平臺&#xff0c;后成為 Apache 基金會的頂級項目 。它不僅僅是一個簡單的消息隊列&#xff0c;更是一個分布式流處理平臺&#xff0c;具備強大的消息隊列、存儲系統…

Lighthouse與首屏優化

之前提到首屏優化&#xff0c;想到的就是Vue項目首頁打開很慢需要優化。一般都是肉眼看看&#xff0c;對當前的加載速度并沒有一個準確的衡量標準&#xff0c;也沒有很清晰的解決思路。 前兩天我想給自己的網站申請谷歌廣告&#xff0c;聽說審核對網站的性能要求很高。于是網上…

Maven 之 打包項目時沒有使用本地倉庫依賴問題

背景 pom 中使用了第三方jar包&#xff0c;遠程倉庫設置的是阿里云&#xff0c;之前運行很好&#xff0c;今天不知道怎么的&#xff0c;打包總是報錯&#xff0c;阿里云倉庫無法找到依賴包(本來也沒有)&#xff0c;按理來說&#xff0c;編譯打包時會優先選擇本地倉庫的包才對&a…

Mysql基礎入門\期末速成

DDL 操作數據庫語句 創建&刪除數據庫語句 創建數據庫 create database 數據庫名稱; -- 直接創建 create database if not exists 數據庫名稱; -- 如果不存在&#xff0c;則創建 create database 數據庫名稱 default charset utf8mb4; -- 創建編譯類型utf8的數據類型 cre…

SCADA|KingSCADA4.0中歷史趨勢控件與之前版本的差異

哈嘍,你好啊,我是雷工! 最近用到KingSCADA4.0信創版本,也算嘗鮮使用。 在使用的過程中發現有些功能或多或少存在一些差異, 這里將遇到的一些不同總結一下,便于后期更好的使用。 01 歷史趨勢控件 在KingSCADA中有一個歷史趨勢曲線控件KSHTrend。 該控件既可以連接King…

ubuntu 拒絕ssh連接,連不上ssh,無法遠程登錄: Connection failed.

目錄 問題描述視窗 可視化桌面命令行 問題描述 [C:\~]$ Connecting to 192.166.8.85:22... Could not connect to 192.166.8.85 (port 22): Connection failed.Type help to learn how to use Xshell prompt. [C:\~]$ Connecting to 192.166.8.85:22... Could not connect to …

【大模型應用開發】向量數據庫向量檢索方法存在問題及優化

一、檢索結果重復 1. 問題分析 在構建向量數據庫時&#xff0c;對文檔分割會存在重復塊&#xff08;chunk_overlap&#xff1a;指兩個塊之間共享的字符數量&#xff0c;用于保持上下文的連貫性&#xff0c;避免分割丟失上下文信息&#xff09;&#xff0c;如下圖所示&#xf…

MySQL常用函數詳解之數值函數

MySQL常用函數詳解之數值函數 一、數值函數概述1.1 數值函數的作用1.2 數值函數分類 二、算術運算函數2.1 加法運算&#xff08;&#xff09;2.2 減法運算&#xff08;-&#xff09;2.3 乘法運算&#xff08;*&#xff09;2.4 除法運算&#xff08;/ 或 DIV&#xff09;2.5 取模…

13、Redis進階二之Redis數據安全性分析

? 、Redis性能壓測腳本介紹 Redis的所有數據是保存在內存當中的&#xff0c; 得益于內存?效的讀寫性能&#xff0c; Redis的性能是?常強悍的 。但 是&#xff0c;內存的缺點是斷電即丟失&#xff0c;所以 &#xff0c;在實際項?中&#xff0c; Redis—旦需要保存—些重要的…

【系統分析師】2011年真題:綜合知識-答案及詳解

文章目錄 【第1題】【第2~3題】【第4~5題】【第6題】【第7~8題】【第9題】【第10題】【第11題】【第12題】【第13題】【第14題】【第15題】【第16題】【第17題】【第18題】【第19~20題】【第21題】【第22題】【第23題】【第24~25題】【第26題】【第27題】【第28題】【第29題】【…

FastAPI-MCP構建自定義MCP工具實操指南

一、簡介 ? FastAPI-MCP是一個基于python FastAPI框架開發的開源項目&#xff0c;可以自動識別并暴露FastAPI接口為MCP工具 ? 擁有FastAPI框架的所有優點&#xff0c;如異步高并發、獨立遠程部署、OpenAPI文檔 ? 提供SSE、mcp-remote接入方式&#xff0c;支持設置授權訪問…

LLMs之Memory:《LLMs Do Not Have Human-Like Working Memory》翻譯與解讀

LLMs之Memory&#xff1a;《LLMs Do Not Have Human-Like Working Memory》翻譯與解讀 導讀&#xff1a;該論文通過三個精心設計的實驗&#xff0c;證明了當前的大型語言模型&#xff08;LLMs&#xff09;缺乏類似人類的工作記憶。實驗結果表明&#xff0c;LLMs無法在沒有明確外…

Node.js驗證碼:從生成到驗證的趣味之旅

文章目錄 Node.js驗證碼&#xff1a;從生成到驗證的趣味之旅&#x1f4dc; 引言&#xff1a;為什么需要驗證碼&#xff1f;1. 驗證碼的基本原理 &#x1f9e0;驗證碼工作流程示意圖 2. 技術棧準備 &#x1f6e0;?3. 驗證碼生成詳解 &#x1f3a8;3.1 生成SVG驗證碼3.2 轉換為P…

芯科科技攜最新Matter演示和參考應用精彩亮相Matter開放日和開發者大會

全面展示賦能Matter設備實現跨協議和跨海內外生態的技術能力 作為Matter標準創始廠商之一和其解決方案的領先供應商&#xff0c;Silicon Labs&#xff08;亦稱“芯科科技”&#xff09;于6月12至13日參加由連接標準聯盟中國成員組&#xff08;CMGC&#xff09;主辦的Matter年度…

AndroidStudio下載的SDK沒有tool目錄,或者想要使用uiautomatorviewer工具

1.如果沒有tool目錄可以使用下面的地址進行下載 https://dl.google.com/android/repository/tools_r25.2.5-windows.zip 2.并且把下載的文件解壓到放在AndroidStudio的目錄中 3.如果使用uiautomatorviewer.bat出現下面的錯誤 Unable to connect to adb.Check if adb is instal…

FastJSON等工具序列化特殊字符時會加轉義字符\

在Java中JSON數據格式用String接收時&#xff0c;此時在FastJSON層面看來該JSON只是普通字符串&#xff0c;所以對原字符串序列化會得到轉義字符\ 得到轉義后字符串&#xff0c;再反序列化轉義后字符串會得到原字符串 String json"{\"name\": \"張三\&quo…