react-routes-dub
Stupid-easy React routing for the browser in 200 lines of code and with a single dependency.
That single dependency is important. path-to-regexp is used by Express to parse path strings, and now you can use it for your client-side routes.
Why you might use it:
- All configuration takes place in a single file, using plain JS.
- Uses the React Context API for minimum fuss.
- Routes are named, as they should be.
Why you might skip it:
- Browser only. Not for React Native, React VR, or anything else like that.
Installation
yarn add react-routes-dub
or
npm install --save react-routes-dub
Usage
Define your routes with a single file:
// routes.js ; const DubProvider Link Route pathFor } = ;
Now everything you need to begin routing is immediately available.
// app.jsx import React Component from 'react';import DubProvider Link Route from './routes'; { return <DubProvider> <Link ='home'>Home</Link> <Link ='pet' =>See Dog One</Link> <Link ='pet' =>See Dog Two</Link> <Link ='pet.toys' =>See Dog Toys</Link> <Route ='home'> <p>Welcome home</p> </Route> <Route ='pet'> params // route parameters are available here return <p>Pet ID here: paramspetId </p> </Route> <Route ='pet.toys'> <p>Pet Toys!</p> </Route> </DubProvider> ; }
That's it! You're done.
See ./test for a complete example.