SkillAgentSearch skills...

Wouter

πŸ₯’ A minimalist-friendly ~2.2KB routing for React and Preact

Install / Use

/learn @molefrog/Wouter

README

<div align="center"> <img src="assets/logo.svg" width="80" alt="Wouter β€” a super-tiny React router (logo by Katya Simacheva)" /> </div> <br /> <div align="center"> <a href="https://npmjs.org/package/wouter"><img alt="npm" src="https://img.shields.io/npm/v/wouter.svg?color=black&labelColor=888" /></a> <a href="https://travis-ci.org/molefrog/wouter"><img alt="CI" src="https://img.shields.io/github/actions/workflow/status/molefrog/wouter/size.yml?color=black&labelColor=888&label=2.5KB+limit" /></a> <a href="https://coveralls.io/github/molefrog/wouter?branch=v3"><img alt="Coverage" src="https://img.shields.io/coveralls/github/molefrog/wouter/v3.svg?color=black&labelColor=888" /></a> <a href="https://www.npmjs.com/package/wouter"><img alt="Coverage" src="https://img.shields.io/npm/dm/wouter.svg?color=black&labelColor=888" /></a> <a href="https://pr.new/molefrog/wouter"><img alt="Edit in StackBlitz IDE" src="https://img.shields.io/badge/StackBlitz-New%20PR-black?labelColor=888" /></a> </div> <div align="center"> <b>wouter</b> is a tiny router for modern React and Preact apps that relies on Hooks. <br /> A router you wanted so bad in your project!<br> </div>

Features

<img src="assets/wouter.svg" align="right" width="250" alt="by Katya Simacheva" />

developers :sparkling_heart: wouter

... I love Wouter. It’s tiny, fully embraces hooks, and has an intuitive and barebones API. I can accomplish everything I could with react-router with Wouter, and it just feels more minimalist while not being inconvenient.

Matt Miller, An exhaustive React ecosystem for 2020

Wouter provides a simple API that many developers and library authors appreciate. Some notable projects that use wouter: Ultra, React-three-fiber, Sunmao UI, Million and many more.

Table of Contents

Getting Started

First, add wouter to your project.

npm i wouter

Or, if you're using Preact the use the following command npm i wouter-preact.

Check out this simple demo app below. It doesn't cover hooks and other features such as nested routing, but it's a good starting point for those who are migrating from React Router.

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>
  </>
);

Browser Support

This library is designed for ES2020+ compatibility. If you need to support older browsers, make sure that you transpile node_modules. Additionally, the minimum supported TypeScript version is 4.1 in order to support route parameter inference.

Wouter API

Wouter comes with three kinds of APIs: low-level standalone location hooks, hooks for routing and pattern matching and more traditional component-based API similar to React Router's one.

You are free to choose whatever works for you: use location hooks when you want to keep your app as small as possible and don't need pattern matching; use routing hooks when you want to build custom routing components; or if you're building a traditional app with pages and navigation β€” components might come in handy.

Check out also FAQ and Code Recipes for more advanced things like active links, default routes, server-side rendering etc.

The list of methods available

Location Hooks

These can be used separately from the main module and have an interface similar to useState. These hooks are standalone and don't include built-in support for nesting, base path, or route matching. However, when passed to <Router>, they work seamlessly with all Router features including nesting and base paths.

Routing Hooks

Import from wouter module.

  • useRoute β€” shows whether or not current page matches the pattern provided.
  • useLocation β€” allows to manipulate current router's location, by default subscribes to browser location. Note: this isn't the same as useBrowserLocation, read below.
  • useParams β€” returns an object with parameters matched from the closest route.
  • useSearch β€” returns a search string – everything that goes after the ?.
  • useRouter β€” returns a global router object that holds the configuration. Only use it if you want to customize the routing.

Components

Import from wouter module.

  • <Route /> β€” conditionally renders a component based on a pattern.
  • <Link /> β€” wraps <a>, allows to perform a navigation.
  • <Switch /> β€” exclusive routing, only renders the first matched route.
  • <Redirect /> β€” when rendered, performs an immediate navigation.
  • <Router /> β€” an optional top-level component for advanced routing configuration.

Hooks API

useRoute: route matching and parameters

Checks if the current location matches the pattern provided and returns an objec

View on GitHub
GitHub Stars7.8k
CategoryDevelopment
Updated7h ago
Forks181

Languages

TypeScript

Security Score

100/100

Audited on Mar 22, 2026

No findings