Super naive dependency free router for React
This library two components : Route and NotFound.
A Route will display its children if an only current browser route matches its path prop.
import { Route } from "naive-router";
// ...
<Route path="/foo/bar">
<div>Matched /foo/bar route</div>
</Route>import { Route } from "naive-router";
// ...
const Child = ({id}) => <div>Matched route "/foo/{id}" with {id}</div>
<Route path="/foo/{id}">
<Child />
</Route>Child of the route component can be a render function. This function will be invoked with two arguments : path and query parameters.
import { Route } from "naive-router";
// ...
const Child = ({id}) => <div>Matched route "/foo/{id}" with {id}</div>
<Route path="/foo/{id}">
{(pathParameters, queryParemeters) => <div>{pathParameters.id} {queryParameters.id}</div>}
</Route>It's possible to display a special route when no Route component has been matched.
import { Route, NotFound } from "naive-router";
// ...
const Child = ({id}) => <div>Matched route "/foo/{id}" with {id}</div>
<>
<Route path="/foo/{id}">
{(pathParameters, queryParemeters) => <div>{pathParameters.id} {queryParameters.id}</div>}
</Route>
<NotFound>
<div>Not found !</div>
</NotFound>
</>As for Route component, it's possible to have more than one NotFound component.
Each one of them will be display when no Route is matched.
This library comes with some pitfalls :
- It monkey-patch
window.historymethods :pushState,go,back,replaceStateandforwardto broadcats any location change to everyRoutecomponent. Routecomponents pass path and query parameters as props to child components. Thus there is a risk of name conflict. The priority order is : 1) path parameters 2) query parameters 3) props. Passing a render function as a children to the Route component solve this issue.- Several
Routecomponents could match a given url (for instance both/foo/barmatches routes/foo/barand/foo/{id}). You should think about theRoutecomponent as a conditional display of its children based on the route.
- Route matching
- Path parameters support
- Query parameters support
- 404 Route
- Nested routes