Skip to content

feat(router): allow type-safe hooks to access ancestor route data#53

Merged
uhyo merged 1 commit intomasterfrom
feat/nested-route-hooks
Jan 24, 2026
Merged

feat(router): allow type-safe hooks to access ancestor route data#53
uhyo merged 1 commit intomasterfrom
feat/nested-route-hooks

Conversation

@uhyo
Copy link
Owner

@uhyo uhyo commented Jan 24, 2026

Summary

  • Enable useRouteParams, useRouteState, and useRouteData to access data from ancestor routes in nested route hierarchies
  • Add parent reference chain to RouteContextValue for traversing route hierarchy
  • Create shared useRouteContext internal hook to reduce code duplication

Problem

When using nested routes like:

const adminUserRoute = route({ id: 'admin.user', ... });
const adminRoute = route({ id: 'admin', ..., children: [adminUserRoute] });

Inside the admin.user page component, calling useRouteState(adminRoute) would throw an error because the hooks only checked the nearest RouteContext and threw if the ID didn't match.

Solution

Added a parent field to RouteContextValue that references the parent route's context. Hooks now traverse this chain to find the route with the matching ID.

Test plan

  • Existing tests pass
  • New tests added for nested route access scenarios
  • TypeScript type checking passes
  • Documentation updated

🤖 Generated with Claude Code

Add parent reference chain to RouteContextValue, enabling useRouteParams,
useRouteState, and useRouteData to access data from ancestor routes in
nested route hierarchies.

Changes:
- Add `parent` field to RouteContextValue for traversing route hierarchy
- Add `findRouteContextById` helper to search ancestor chain
- Create shared `useRouteContext` internal hook to reduce duplication
- Update hooks to traverse parent chain when looking for route by ID
- Update error messages from "mismatch" to "not found in hierarchy"
- Add tests for nested route access scenarios
- Update documentation to reflect new capability

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@uhyo uhyo merged commit 2e3697e into master Jan 24, 2026
1 check passed
@uhyo uhyo deleted the feat/nested-route-hooks branch January 24, 2026 01:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant