🐁 react-tiniest-router
For the times when you really need a simple router.
Based on mobx-router and rttr.
Usage
- Write the routes object.
const routes = {
home: {
id: 'home',
path: '/',
},
about: {
id: 'about',
path: '/about',
},
gallery: {
id: 'gallery',
path: '/gallery/:imageId',
},
}
- Wrap your app with the Router component
<Router routes={routes}>
<App />
</Router>
- Use the router using
useRouter
- Use the
goTo
function for navigating to a route - Use the
isRoute
function for checking if a route is currently active
const Root = () => {
const {goTo, isRoute} = useRouter();
return (
<div>
<div>
<button onClick={() => goTo(routes.home)}>go home</button>
<button onClick={() => goTo(routes.about)}>go to about</button>
<button onClick={() => goTo(routes.gallery, { imageId: 1 })}>
go to picture 1
</button>
<button onClick={() => goTo(routes.gallery, { imageId: 2 })}>
go to picture 2
</button>
<button onClick={() => goTo(routes.gallery, { imageId: 3 })}>
go to picture 3
</button>
</div>
<br/>
{isRoute(routes.home) && <div>Welcome home</div>}
{isRoute(routes.about) && <div>About us</div>}
{isRoute(routes.gallery) && <Gallery />}
</div>
);
};
- You also get
params
, queryParams
, routeId
, path
in the router object.
const Gallery = () => {
const { params } = useRouter();
return <div>Browsing picture {params.imageId}</div>;
};
- Use modifiers to create special parameters.
const routes = {
zeroOrMore: {
id: 'zeroOrMore',
path: '/:foo*',
},
oneOrMore: {
id: 'oneOrMore',
path: '/:bar+',
},
optional: {
id: 'optional',
path: '/:bar?',
},
emptyString: {
id: 'emptyString',
path: /:qux@/:quuz
}
See path-to-regexp for more details.
FAQ
-
Does it support optional parameters in the path definition?
Not yet, but it will as soon as I need them in a project.
-
Does it support SSR?
No.
-
Will it ever support SSR?
NO.
-
Does it have tests?
TypeScript is poor man's tests.
-
Will it ever have tests?
If you write them.
-
Does it support code splitting?
Did you see which repo you're actually browsing?
Does it say "facebook" in the url? No. So, no.
-
Does it support async routes?
Please stop doing stupid stuff with your router.
-
Does it support protected routes?
Please stop doing stupid stuff with your router.
-
I'm offended by this FAQ section, where can I complain?
Yell @ me on Twitter
- 🏫 React Academy - Interactive React and GraphQL workshops
- 💌 Twizzy - A standalone app for Twitter DM
- 🤖 JSUI - A powerful UI toolkit for managing JavaScript apps
- 📹 Vlog - Watch my sad developer life