react-router-query
An API to query static react-router routes.
Installation
npm install react-router-query
Usage
import query from 'react-router-query'; const routes = <Route ="/" => <IndexRoute = /> <Route ="about" = /> <Route ="inbox" => <Redirect ="messages/:id" ="/messages/:id" /> </Route> <Route => <Route ="messages/:id" = /> </Route> </Route>; ;
The query
function will traverse the route configuration and call the provided callback with an array of FlatRoute
s: leaf routes from the passed route configuration, with additional properties fullPath
(the full path to the leaf route) and parents
(an array of parents of the leaf route).
This can be useful for static sites using react-router, where you want to render a list of all pages under a given path.
react-router-query also provides a withQuery
higher-order component for React, for cases when you want to render query results to a component.
// PostsList.jsximport query withQueries from 'react-router-query'; { return <div ="posts"> posts </div> ;} const queries = posts: ; queriesPostsList;
A withQuery
component must be rendered inside a RoutesProvider
, e.g.
// app.jsx import render from 'react-dom';import Router from 'react-router';import PostsList from './PostsList';// ... const routes = <Route ="/" => <IndexRoute = /> <Route ="posts"> <Route ="post1" = = /> <Route ="post2" = = /> </Route> </Route>; ;