Configs2026年5月25日·1 分钟阅读

Wouter — Minimalist 2KB Router for React and Preact

A tiny (~2.2KB) routing library for React and Preact that provides hooks-first API with nested routes, pattern matching, and SSR support without the weight of larger routers.

Agent 就绪

复制一段可直接交给 Agent 执行的安装路径

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Wouter Overview
直接安装命令
npx -y tokrepo@latest install a09d5611-5857-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

Introduction

Wouter is a minimalist routing library for React and Preact weighing only 2.2KB. It provides a hooks-first API inspired by React Router but with a much smaller footprint and zero dependencies. It handles nested routes, wildcard patterns, hash-based routing, and server-side rendering.

What Wouter Does

  • Matches URL paths to components using a declarative Route API
  • Provides hooks like useRoute, useLocation, and useParams for programmatic routing
  • Supports nested routing and base path configuration for sub-applications
  • Handles wildcard and named parameter patterns in route paths
  • Works with hash-based URLs and custom location hooks for memory routing

Architecture Overview

Wouter uses a single location hook (useLocation) as its source of truth, subscribing to the browser History API. Route matching uses a path-to-regexp style pattern compiler. Components re-render only when their specific route match changes, avoiding unnecessary updates. The library ships no context providers by default; the Router wrapper is only needed for base paths or custom location hooks.

Self-Hosting & Configuration

  • Install via npm: npm install wouter
  • Import Route, Link, Switch, and hooks directly from 'wouter'
  • For Preact, import from 'wouter-preact' instead
  • Configure base paths with <Router base="/app"> for sub-applications
  • Use memoryLocation for testing or non-browser environments

Key Features

  • Only 2.2KB gzipped with zero dependencies
  • Hooks-first design with useRoute, useLocation, useParams, and useSearch
  • Full TypeScript support with typed route parameters
  • Works with React 16.8+, React 18, and Preact
  • SSR-compatible with server-side location hooks

Comparison with Similar Tools

  • React Router — full-featured router with loaders and actions; much larger bundle at 13KB+
  • TanStack Router — type-safe with built-in search params; heavier, more opinionated
  • Next.js App Router — file-system based routing; tied to the Next.js framework
  • @reach/router — accessible-first router; merged into React Router v6
  • navigo — vanilla JS router; no React integration

FAQ

Q: Can Wouter replace React Router in an existing project? A: For most use cases, yes. Wouter covers Route, Link, Switch, Redirect, and common hooks. Advanced features like loaders or data fetching are not included.

Q: Does Wouter support lazy-loaded routes? A: Yes. Wrap your route components with React.lazy and Suspense as you would with any React component.

Q: How do I access query parameters? A: Use the useSearch hook, which returns the raw search string. Parse it with URLSearchParams.

Q: Is Wouter compatible with React Server Components? A: Wouter hooks use client-side state, so route components using hooks need the "use client" directive in RSC environments.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产