npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@0bipinnata0/router

v0.0.17

Published

[![npm package][npm-badge]][npm] [![build][build-badge]][build]

Readme

npm package build

React Router 实现

路由的三种实现方式

本项目实现了三种不同的路由方式,每种方式都有其特定的使用场景:

  1. BrowserRouter (location.pathname)

    • 使用 HTML5 History API
    • 通过监听 popstate 事件实现路由状态同步
    • 使用 history.pushState 更新 URL
    • 适合需要干净URL的现代Web应用
  2. HashRouter (location.hash)

    • 基于 URL hash 实现路由
    • 通过修改 location.hash 触发路由变化
    • 兼容性好,适合不支持 History API 的旧浏览器
  3. MemoryRouter (localStorage)

    • 使用 localStorage 存储路由状态
    • 完全基于内存的路由实现
    • 适合非浏览器环境或需要持久化路由状态的场景

核心实现原理

状态管理

  1. 状态初始化

    • BrowserRouter: 使用 location.pathname
    • HashRouter: 使用 location.hash
    • MemoryRouter: 使用 localStorage 存储的路径
  2. 状态变更机制

    • 采用发布订阅模式
    • 通过 routeEmit 统一管理路由事件
    • 各路由组件监听状态变化并更新UI

组件体系

  1. 路由容器组件

    • BrowserRouter/HashRouter/MemoryRouter
    • 负责路由状态的初始化和更新
    • 提供路由上下文
  2. Routes 组件

    • 收集和管理 Route 子组件
    • 监听路由变化并更新UI
    • 实现路由匹配和渲染
  3. Route 组件

    • 定义路由路径和对应组件
    • 支持嵌套路由
    • 支持路由参数
  4. Link 组件

    • 提供声明式的路由导航
    • 触发路由状态更新
  5. Outlet 组件

    • 渲染子路由内容
    • 实现路由嵌套

路由匹配逻辑

  1. 路由校验

    • 过滤非 Route 组件
    • 支持权限控制
  2. 路径匹配

    • 支持精确匹配和模式匹配
    • 处理路由参数
    • 支持通配符路由(404页面)

使用示例

<Router>
  <Link to="/">主页</Link>
  <Link to="/login">登录</Link>
  
  <Routes>
    <Route path="/login" element={<Login />} />
    <Route path="/" element={<Home />}>
      <Route path="course" element={<Course />}>
        <Route path=":id" element={<Detail />} />
      </Route>
    </Route>
  </Routes>
</Router>

API 参考

  • BrowserRouter: 基于 History API 的路由实现
  • HashRouter: 基于 URL hash 的路由实现
  • MemoryRouter: 基于 localStorage 的路由实现
  • Link: 路由导航组件
  • Route: 路由配置组件
  • Routes: 路由容器组件
  • Outlet: 子路由渲染组件
  • useParams: 获取路由参数 Hook
  • useNavigate: 编程式导航 Hook