T8 React Router
Nested routes
Nested routes don't require special handling. All routes are handled equally and independently from each other.
let App = () => {
let { at } = useRoute();
return (
<>
{at("/about", <About/>)}
{at("/about/contacts", <Contacts/>)}
// ...
</>
);
};
In a type-safe setup, a URL schema of a nested route can inherit certain parameters from the parent route. Such relations (which might as well be other than direct nestedness) can be settled within the URL schema with the schema toolset.
import { createURLSchema } from "url-shape";
import { z } from "zod";
let sectionParams = z.object({
sectionId: z.coerce.number(),
});
export const { url } = createURLSchema({
"/posts/:sectionId": z.object({
params: sectionParams,
}),
"/posts/:sectionId/stories/:storyId": z.object({
params: z.object({
...sectionParams.shape, // Shared params
storyId: z.string(),
}),
}),
});
Such a setup doesn't impose specific implicit relations between the routes (like parameter inheritance) ahead of time. The relations between the routes, as arbitrary as they can be, are seen and managed directly, allowing for fine-grained control, including sharing or filtering out certain parameters, without the need to work around implicit constraints.
← Type safety | URL provider →