Wouter
π₯’ A minimalist-friendly ~2.2KB routing for React and Preact
Install / Use
/learn @molefrog/WouterREADME
Features
<img src="assets/wouter.svg" align="right" width="250" alt="by Katya Simacheva" />- Minimum dependencies, only 2.1 KB gzipped vs 18.7KB React Router.
- Supports both React and Preact! Read "Preact support" section for more details.
- No top-level
<Router />component, it is fully optional. - Mimics React Router's best practices by providing
familiar
Route,Link,SwitchandRedirectcomponents. - Has hook-based API for more granular control over routing (like animations):
useLocation,useRouteanduseRouter.
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.
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
-
- I deploy my app to the subfolder. Can I specify a base path?
- How do I make a default route?
- How do I make a link active for the current route?
- Are strict routes supported?
- Are relative routes and links supported?
- Can I initiate navigation from outside a component?
- Can I use wouter in my TypeScript project?
- How can add animated route transitions?
- How do I add view transitions to my app?
- Preact support?
- Server-side Rendering support (SSR)?
- How do I configure the router to render a specific route in tests?
- 1KB is too much, I can't afford it!
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.
import { useBrowserLocation } from "wouter/use-browser-location"β allows to manipulate current location in the browser's address bar, a tiny wrapper around the History API.import { useHashLocation } from "wouter/use-hash-location"β similarly, gets location from the hash part of the address, i.e. the string after a#.import { memoryLocation } from "wouter/memory-location"β an in-memory location hook with history support, external navigation and immutable mode for testing. Note the module name because it is a high-order hook. See how memory location can be used in testing.
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 asuseBrowserLocation, 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
