Skip to content

Commit 13d38af

Browse files
committed
docs: remove .route("/")
1 parent 6217024 commit 13d38af

1 file changed

Lines changed: 22 additions & 23 deletions

File tree

README.md

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,9 @@ TypeRoute requires React 18 or higher.
169169

170170
# Motivation
171171

172-
Most React routers today either lack type safety entirely, or achieve it through build plugins and code generation. TypeRoute takes a different path: it uses TypeScript's own inference to give you full autocompletion and type checking - for routes, params, search params, navigation - without any tooling beyond the TypeScript compiler you're already running.
172+
Most React routers today either lack type safety entirely, or achieve it through build plugins and code generation. TypeRoute takes a different path: it gives you full autocompletion and type checking - for routes, params, search params, navigation - without any tooling beyond the TypeScript compiler you're already running.
173173

174-
The API is deliberately small. You define routes with a builder, register them once through module augmentation, and that's it. Routes nest, middlewares compose, and types inherit down the tree. There's no config file, no CLI, no codegen step. The whole thing ships at ~4kB gzipped before tree-shaking.
174+
The API is deliberately small. You define routes with a builder, register them once through module augmentation, and that's it. Routes nest and types inherit down the tree. There's no config file, no CLI, no codegen step. The whole thing ships at ~4kB gzipped before tree-shaking.
175175

176176
TypeRoute doesn't try to be a framework. It doesn't own your data fetching, your file structure, or force you into SSR. It handles routing - matching URLs to components and managing navigation - and stays out of the way for everything else.
177177

@@ -215,19 +215,19 @@ Route building is immutable: every method on a route returns a new route instanc
215215

216216
# Nested routes and layouts
217217

218-
Any route can have child routes. Call `.route()` on an existing route to create one:
218+
Any route can have child routes:
219219

220220
```tsx
221221
const dashboard = route("/dashboard").component(DashboardLayout);
222222

223-
const overview = dashboard.route("/").component(Overview);
223+
const overview = dashboard.component(Overview);
224224
const settings = dashboard.route("/settings").component(Settings);
225225
const profile = dashboard.route("/profile").component(Profile);
226226
```
227227

228-
Child routes build on their parent's path. So `overview` matches `/dashboard`, `settings` matches `/dashboard/settings`, and `profile` matches `/dashboard/profile`.
228+
`.route()` extends the path: `settings` matches `/dashboard/settings` and `profile` matches `/dashboard/profile`. Chaining `.component()` directly (like `overview`) doesn't change the path, so `overview` matches `/dashboard`.
229229

230-
They also nest inside the parent's component. The parent renders an `<Outlet />` to mark where child routes should appears:
230+
All of them nest inside `dashboard`'s component. The parent renders an `<Outlet />` to mark where child routes should appear:
231231

232232
```tsx
233233
function DashboardLayout() {
@@ -273,11 +273,11 @@ Beyond paths and components, child routes also inherit search param validators,
273273
Before setting up the router, you need to collect your navigable routes into a collection (either array or record). When building nested route hierarchies, you'll often create intermediate parent routes solely for grouping and shared layouts. These intermediate routes shouldn't be included in your routes collection - only the final, navigable routes should be:
274274

275275
```tsx
276-
// Intermediate route used for hierarchy
276+
// Intermediate route used for shared layout
277277
const layout = route("/").component(Layout);
278278

279279
// Navigable routes that users can actually visit
280-
const home = layout.route("/").component(Home);
280+
const home = layout.component(Home);
281281
const about = layout.route("/about").component(About);
282282

283283
// Collect only the navigable routes
@@ -287,7 +287,7 @@ const routes = [home, about]; // ✅ Don't include `layout`
287287
const routes = { home, about };
288288
```
289289

290-
This makes sure that only actual pages can be matched and appear in autocomplete. The intermediate routes still exist as part of the hierarchy, they just aren't directly navigable. Note that the order of routes in the collection doesn't matter - TypeRoute uses a [ranking algorithm](#route-matching-and-ranking) to pick the most specific match.
290+
This makes sure that only actual pages can be matched and appear in autocomplete. The intermediate routes still exist as part of the build chain, they just aren't directly navigable. Note that the order of routes in the collection doesn't matter - TypeRoute uses a [ranking algorithm](#route-matching-and-ranking) to pick the most specific match.
291291

292292
The `RouterRoot` component is the entry point to TypeRoute. It listens to URL changes, matches the current path against your routes, and renders the matching route's component hierarchy.
293293

@@ -792,16 +792,22 @@ Note that `Navigate` uses `useLayoutEffect` internally to ensure the navigation
792792

793793
# Index routes
794794

795-
Layout routes often need a child route at `"/"` just to show default content at the parent's path:
795+
When you have a layout route, you often want content at the layout's path when no child route matches. Since `.component()` doesn't change the path, you create an index route by chaining directly on the layout:
796796

797797
```tsx
798798
const dashboard = route("/dashboard").component(DashboardLayout);
799799

800-
const overview = dashboard.route("/").component(Overview);
800+
const overview = dashboard.component(Overview);
801801
const settings = dashboard.route("/settings").component(Settings);
802802
```
803803

804-
This is perfectly fine, but `.index()` offers a shorthand. It defines what renders when no child route matches, directly on the parent:
804+
Here, `overview` matches `/dashboard` and renders `DashboardLayout > Overview`. Only `overview` and `settings` go in the routes collection - `dashboard` is just an intermediate used for building:
805+
806+
```tsx
807+
const routes = [overview, settings];
808+
```
809+
810+
`.index()` offers an alternative that combines the layout and the index content into a single navigable route:
805811

806812
```tsx
807813
const dashboard = route("/dashboard")
@@ -811,14 +817,7 @@ const dashboard = route("/dashboard")
811817
const settings = dashboard.route("/settings").component(Settings);
812818
```
813819

814-
Here's what renders at each path:
815-
816-
```
817-
/dashboard → DashboardLayout > Overview
818-
/dashboard/settings → DashboardLayout > Settings
819-
```
820-
821-
Under the hood, `.index(Comp)` is equivalent to `.component(() => useOutlet() ?? <Comp />)`. Note that when using `.index()`, the layout route itself becomes navigable. Include it in your routes collection instead of the former child route:
820+
Under the hood, `.index(Comp)` is equivalent to `.component(() => useOutlet() ?? <Comp />)`. It renders `Overview` when no child route matches, and the child's outlet when one does. Since the layout route is now navigable, include it in your routes collection:
822821

823822
```tsx
824823
const routes = [dashboard, settings];
@@ -1264,7 +1263,7 @@ function AppLayout() {
12641263
}
12651264

12661265
// Page routes
1267-
const home = app.route("/").component(() => <h1>Welcome home</h1>);
1266+
const home = app.component(() => <h1>Welcome home</h1>);
12681267
const about = app.route("/about").component(() => <h1>About us</h1>);
12691268

12701269
// Router setup
@@ -1351,7 +1350,7 @@ This also works for scoped not-found pages. If you want a fallback specific to a
13511350
```tsx
13521351
const dashboard = route("/dashboard").component(DashboardLayout);
13531352

1354-
const overview = dashboard.route("/").component(Overview);
1353+
const overview = dashboard.component(Overview);
13551354
const settings = dashboard.route("/settings").component(Settings);
13561355
const dashboardNotFound = dashboard.route("/*").component(DashboardNotFound);
13571356
```
@@ -1536,7 +1535,7 @@ Use [route handles](#route-handles) to define page titles and update the browser
15361535

15371536
```tsx
15381537
const layout = route("/").handle({ title: "App" }).component(Layout);
1539-
const home = layout.route("/").handle({ title: "Home" }).component(HomePage);
1538+
const home = layout.handle({ title: "Home" }).component(HomePage);
15401539
const settings = layout
15411540
.route("/settings")
15421541
.handle({ title: "Settings" })

0 commit comments

Comments
 (0)