Drop-in named route support for React Router.
Enhance your history with
useNamedRoutes and pass in your routes:
;;;/* ... */const routes =<Route path="/"><Route name="widgets" path="widgets" component=WidgetsPage><Route name="widget" path=":widgetId" component=WidgetPage /></Route></Route>;// The order of history enhancers matters here.const history = routes ;ReactDOM;
You can then use either route names or objects with
name and optionally
This also works with
<Link to="widgets"><Linkto= name: 'widget' params: widgetId: 'bar'activeClassName="active">
Normal paths will also still work:
$ npm i -S react-router history@2 use-named-routes
use-named-routes works with both JSX and plain route definitions. Define
name on the route element or object to assign it a name.
use-named-routes ignores dynamic routes under
getIndexRoute. You probably shouldn't be using those anyway – use
getComponents if you want code splitting.
Location descriptor strings are treated as route names, except when the location descriptor begins with a
/, in which case the location descriptor is passed to the base history as-is.
Location descriptor objects are treated as describing a named route if they have a
name value. Any parameters in the path for the named route are populated from the
params value. If no
name value is present, the location descriptor is passed to the base history as-is.
In either case, if a name is present, the formatted pattern is passed to the base history as
pathname on a location descriptor object. Other properties on the location descriptor are passed through as-is.