-
-
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
Open
thomastvedt
wants to merge
3
commits into
TanStack:main
Choose a base branch
from
thomastvedt:feat/use-pending-matches
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| // ... | ||
| } | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.