当前位置: 首页 > news >正文

手写简版React-router

手写一个简化版本的 React Router,主要实现其核心功能,包括路由配置、路径匹配和导航。

这个简化版本将包括以下组件和函数:

1. BrowserRouter

2. Routes 和 Route

3. Link

4. useRoutes

5. useParams

1. 基本框架和组件

1.1. BrowserRouter

BrowserRouter 组件使用 HTML5 History API,并为应用提供路由上下文。

import React, { useState, useEffect, createContext, useContext } from 'react';const RouterContext = createContext();function BrowserRouter({ children }) {const [location, setLocation] = useState(window.location.pathname);useEffect(() => {const handlePopState = () => setLocation(window.location.pathname);window.addEventListener('popstate', handlePopState);return () => window.removeEventListener('popstate', handlePopState);}, []);const navigate = (to) => {window.history.pushState({}, '', to);setLocation(to);};return (<RouterContext.Provider value={{ location, navigate }}>{children}</RouterContext.Provider>);
}

1.2. Routes 和 Route

Routes 组件遍历所有子路由,并匹配当前路径,Route 组件定义路径和对应的组件。

function Routes({ children }) {const { location } = useContext(RouterContext);let element;React.Children.forEach(children, (child) => {if (!element && React.isValidElement(child) && matchPath(child.props.path, location)) {element = child;}});return element;
}function Route({ path, element }) {return element;
}

1.3. Link

Link 组件用于导航。

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

1.4. useRoutes

useRoutes 是一个 hook,用于在函数组件中使用路由。

function useRoutes(routes) {const { location } = useContext(RouterContext);for (let route of routes) {if (matchPath(route.path, location)) {return route.element;}}return null;
}

1.5. useParams

useParams 是一个 hook,用于获取路径参数。

function useParams() {const { location } = useContext(RouterContext);const path = location;const match = path.match(/\/([^\/]+)\/?/);return match ? { id: match[1] } : {};
}

2. 路径匹配函数

matchPath 用于检查路径是否匹配。

function matchPath(pattern, pathname) {const regex = new RegExp(`^${pattern.replace(/:\w+/g, '([^/]+)')}$`);return regex.test(pathname);
}

3. 示例应用

基础定义完成,我们在 React 中使用一下这个 router 。

import React from 'react';
import ReactDOM from 'react-dom';function Home() {return <h2>Home</h2>;
}function About() {return <h2>About</h2>;
}function User() {const { id } = useParams();return <h2>User ID: {id}</h2>;
}function App() {return (<BrowserRouter><nav><Link to="/">Home</Link><Link to="/about">About</Link><Link to="/user/1">User 1</Link></nav><Routes><Route path="/" element={<Home />} /><Route path="/about" element={<About />} /><Route path="/user/:id" element={<User />} /></Routes></BrowserRouter>);
}ReactDOM.render(<App />, document.getElementById('root'));

这个简化版的 React Router,主要实现其核心功能,包括路由配置、路径匹配和导航。省略了很多边缘情况和优化,从本例中可以窥见 react-router 框架整体实现最核心的思路。

4. 补充资料

  • 官方文档:React Router Official Documentation

  • react-router-dom 多类型历史:https://github.com/remix-run/react-router/blob/acc2b94088835d1247bdf3a3f883f74cc3570a0/packages/react-router-dom/index.tsx#L262

  • wouter: https://github.com/molefrog/wouter

  • @tanstack/router:TanStack Router

  • 浏览器历史记录协议:History API - Web APIs | MDN

相关文章:

  • 2025ICRA 最佳论文解读:麻省理工PolyTouch:一种多模态触觉传感器以及基于触觉扩散策略的接触丰富操作方法
  • 【学习笔记】深入理解Java虚拟机学习笔记——第9章 类加载及执行子系统的案例与实战
  • C语言进阶:深度解剖数据在内存中的存储(浮点型在内存中的存储)
  • Linux系统firewall-offline-cmd命令在企业网络安全防护中的应用案例分析
  • 2.RMII的时钟模式
  • Python训练营打卡 Day54
  • 【LangChain】5 评估
  • WebAssembly 2.0:超越浏览器的全栈计算革命
  • 修改Rocky Linux文件夹颜色
  • LLM对话框项目 EventSource封装和MessageServiceClass流式展示封装
  • Spring Boot的Security安全控制——应用SpringSecurity!
  • 关于脉冲功率技术的认识
  • 子集筛选(Select by Data Index)组件研究
  • 贝塞尔曲线的切矢量
  • Java事务隔离问题详解:脏读、不可重复读与幻读(含解决方案)
  • 【算法 day03】LeetCode 203.移除链表元素 | 707.设计链表 | 206.反转链表
  • 【Elasticsearch】文档(一):新增 删除
  • db2主从同步 逻辑复制 APPLY_THROTTLE参数
  • 【CompletableFuture】基础Future (一)
  • 车载诊断框架 --- TCP window size设置
  • 网页小游戏网址大全/seo的中文含义
  • 有那些做自媒体短视频的网站/免费网站分析seo报告是坑吗
  • 杭州网站建设/热点军事新闻
  • 云计算 网站建设/网站建设费用都选网络
  • 网站制作公司商丘市/seo网络搜索引擎优化
  • 阿里云可以做电影网站/培训心得体会模板