-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat: add usePendingMatches hook #7863
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| --- | ||
| id: usePendingMatchesHook | ||
| title: usePendingMatches hook | ||
| --- | ||
|
|
||
| The `usePendingMatches` hook returns the [`RouteMatch`](./RouteMatchType.md) objects for the location the router is currently navigating to. While a navigation is in flight — running `beforeLoad`, loading code-split chunks, or awaiting loaders — these are the matches for the destination location. Once the navigation resolves (or when no navigation is in flight), the array is empty and the resolved matches are available via [`useMatches`](./useMatchesHook.md). | ||
|
|
||
| This is useful for optimistic UI during navigation, e.g. highlighting the navigation item of the destination route from its `staticData` before its chunks and loaders have finished. | ||
|
|
||
| > [!TIP] | ||
| > If you want the currently rendered matches, use [`useMatches`](./useMatchesHook.md) instead. | ||
|
|
||
| ## usePendingMatches options | ||
|
|
||
| The `usePendingMatches` hook accepts a single _optional_ argument, an `options` object. | ||
|
|
||
| ### `opts.select` option | ||
|
|
||
| - Optional | ||
| - `(matches: RouteMatch[]) => TSelected` | ||
| - If supplied, this function will be called with the pending route matches and the return value will be returned from `usePendingMatches`. This value will also be used to determine if the hook should re-render its parent component using shallow equality checks. | ||
|
|
||
| ### `opts.structuralSharing` option | ||
|
|
||
| - Type: `boolean` | ||
| - Optional | ||
| - Only supported by `@tanstack/react-router`. | ||
| - Configures whether structural sharing is enabled for the value returned by `select`. | ||
| - See the [Render Optimizations guide](../../guide/render-optimizations.md) for more information. | ||
|
|
||
| ## usePendingMatches returns | ||
|
|
||
| - If a `select` function is provided, the return value of the `select` function. | ||
| - If no `select` function is provided, an array of [`RouteMatch`](./RouteMatchType.md) objects. The array is empty when no navigation is in flight. | ||
|
|
||
| ## Examples | ||
|
|
||
| ```tsx | ||
| import { useMatches, usePendingMatches } from '@tanstack/react-router' | ||
|
|
||
| function ActiveTab() { | ||
| const pendingTab = usePendingMatches({ | ||
| select: (matches) => matches.findLast((m) => m.staticData.tab)?.staticData.tab, | ||
| }) | ||
| const resolvedTab = useMatches({ | ||
| select: (matches) => matches.findLast((m) => m.staticData.tab)?.staticData.tab, | ||
| }) | ||
|
|
||
| const activeTab = pendingTab ?? resolvedTab | ||
| // ... | ||
| } | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -218,6 +218,23 @@ export function useMatches< | |||||||||||||
| }) as Solid.Accessor<UseMatchesResult<TRouter, TSelected>> | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| export function usePendingMatches< | ||||||||||||||
| TRouter extends AnyRouter = RegisteredRouter, | ||||||||||||||
| TSelected = unknown, | ||||||||||||||
| >( | ||||||||||||||
| opts?: UseMatchesBaseOptions<TRouter, TSelected>, | ||||||||||||||
| ): Solid.Accessor<UseMatchesResult<TRouter, TSelected>> { | ||||||||||||||
| const router = useRouter<TRouter>() | ||||||||||||||
| return Solid.createMemo((prev: TSelected | undefined) => { | ||||||||||||||
| const matches = router.stores.pendingMatches.get() as Array< | ||||||||||||||
| MakeRouteMatchUnion<TRouter> | ||||||||||||||
| > | ||||||||||||||
| const res = opts?.select ? opts.select(matches) : matches | ||||||||||||||
| if (prev === undefined) return res | ||||||||||||||
| return replaceEqualDeep(prev, res) as any | ||||||||||||||
|
Comment on lines
+233
to
+234
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win Add curly braces and improve type safety. As per coding guidelines, control statements like 🐛 Proposed fix- if (prev === undefined) return res
- return replaceEqualDeep(prev, res) as any
+ if (prev === undefined) {
+ return res
+ }
+ return replaceEqualDeep(prev, res) as UseMatchesResult<TRouter, TSelected>📝 Committable suggestion
Suggested change
🤖 Prompt for AI AgentsSource: Coding guidelines
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The typed cast doesn't compile here:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The only remaining part of the finding is the project’s curly-brace style requirement; no type-cast change is needed in this PR. ✏️ Learnings added
|
||||||||||||||
| }) as Solid.Accessor<UseMatchesResult<TRouter, TSelected>> | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| export function useParentMatches< | ||||||||||||||
| TRouter extends AnyRouter = RegisteredRouter, | ||||||||||||||
| TSelected = unknown, | ||||||||||||||
|
|
||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.