How route nesting works
devRoutes can be nested to create hierarchical URL structures. Child routes inherit properties from their parents. You can define children in two ways:
- Inlined: Defining
childrendirectly within theroute()configuration. - Uninlined: Defining a route separately and passing it to the
childrenproperty of another route. Note that an uninlined route is a standalone route and does not automatically know about its parent's context unless it is explicitly assigned as a child.
Important: Child route names cannot start with $ to avoid conflicts with the library's internal API.
import { route } from "react-router-typesafe-routes";
// Inlined nesting
const user = route({
path: "user/:id",
children: { details: route({ path: "details" }) }
});
console.log(user.$path()); // "/user/:id"
console.log(user.details.$path()); // "/user/:id/details"
// Uninlined nesting
const details = route({ path: "details" });
const user = route({ path: "user/:id", children: { details } });
console.log(user.details.$path()); // "/user/:id/details"
console.log(details.$path()); // "/details"