-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Make useBlocker exception on unknown routes optional #4965
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
0865136
0b4ccc4
9e08563
36dcae1
c14c387
2ac23a7
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 | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -29,8 +29,8 @@ interface ShouldBlockFnLocation<...> { | |||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
type ShouldBlockFnArgs = { | ||||||||||||||||||
current: ShouldBlockFnLocation | ||||||||||||||||||
next: ShouldBlockFnLocation | ||||||||||||||||||
current: ShouldBlockFnLocation | undefined | ||||||||||||||||||
next: ShouldBlockFnLocation | undefined | ||||||||||||||||||
action: HistoryAction | ||||||||||||||||||
} | ||||||||||||||||||
``` | ||||||||||||||||||
|
@@ -53,6 +53,12 @@ type ShouldBlockFnArgs = { | |||||||||||||||||
- Type: `boolean` | ||||||||||||||||||
- Specify if the resolver returned by the hook should be used or whether your `shouldBlockFn` function itself resolves the blocking | ||||||||||||||||||
|
||||||||||||||||||
### `options.throwOnUnknownRoute` option | ||||||||||||||||||
|
||||||||||||||||||
- Optional - defaults to `true` | ||||||||||||||||||
- Type: `boolean` | ||||||||||||||||||
- Specify if the hook should throw an exception when either the current or next locations is unkown | ||||||||||||||||||
|
||||||||||||||||||
### `options.blockerFn` option (⚠️ deprecated) | ||||||||||||||||||
|
||||||||||||||||||
- Optional | ||||||||||||||||||
|
@@ -120,7 +126,7 @@ function MyComponent() { | |||||||||||||||||
{/* ... */} | ||||||||||||||||||
{status === 'blocked' && ( | ||||||||||||||||||
<div> | ||||||||||||||||||
<p>You are navigating to {next.pathname}</p> | ||||||||||||||||||
<p>You are navigating to {next?.pathname ?? 'Unknown Path'}</p> | ||||||||||||||||||
<p>Are you sure you want to leave?</p> | ||||||||||||||||||
<button onClick={proceed}>Yes</button> | ||||||||||||||||||
<button onClick={reset}>No</button> | ||||||||||||||||||
|
@@ -138,7 +144,7 @@ import { useBlocker } from '@tanstack/react-router' | |||||||||||||||||
function MyComponent() { | ||||||||||||||||||
const { proceed, reset, status } = useBlocker({ | ||||||||||||||||||
shouldBlockFn: ({ next }) => { | ||||||||||||||||||
return !next.pathname.includes('step/') | ||||||||||||||||||
return next && !next.pathname.includes('step/') | ||||||||||||||||||
}, | ||||||||||||||||||
Comment on lines
146
to
148
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. Return type bug: expression can return
- return next && !next.pathname.includes('step/')
+ // If `next` is unknown, do not block
+ return next ? !next.pathname.includes('step/') : false 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||
withResolver: true, | ||||||||||||||||||
}) | ||||||||||||||||||
|
@@ -170,7 +176,7 @@ function MyComponent() { | |||||||||||||||||
|
||||||||||||||||||
useBlocker({ | ||||||||||||||||||
shouldBlockFn: ({ next }) => { | ||||||||||||||||||
if (next.pathname.includes('step/')) { | ||||||||||||||||||
if (next?.pathname.includes('step/')) { | ||||||||||||||||||
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. Optional chaining is incomplete; potential runtime error.
- if (next?.pathname.includes('step/')) {
+ if (next?.pathname?.includes('step/')) { 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||
return false | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
|
@@ -195,10 +201,10 @@ function MyComponent() { | |||||||||||||||||
const { proceed, reset, status } = useBlocker({ | ||||||||||||||||||
shouldBlockFn: ({ current, next }) => { | ||||||||||||||||||
if ( | ||||||||||||||||||
current.routeId === '/editor-1' && | ||||||||||||||||||
next.fullPath === '/foo/$id' && | ||||||||||||||||||
next.params.id === '123' && | ||||||||||||||||||
next.search.hello === 'world' | ||||||||||||||||||
current?.routeId === '/editor-1' && | ||||||||||||||||||
next?.fullPath === '/foo/$id' && | ||||||||||||||||||
next?.params.id === '123' && | ||||||||||||||||||
next?.search.hello === 'world' | ||||||||||||||||||
) { | ||||||||||||||||||
return true | ||||||||||||||||||
} | ||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix typo and grammar in the
throwOnUnknownRoute
option description.“locations is unkown” → “location cannot be resolved” (and singular agreement). This is user-facing; fix it.
📝 Committable suggestion
🧰 Tools
🪛 LanguageTool
[grammar] ~58-~58: There might be a mistake here.
Context: ...Route
option - Optional - defaults to
true- Type:
boolean` - Specify if the hook sh...(QB_NEW_EN)
[grammar] ~59-~59: There might be a mistake here.
Context: ...- Optional - defaults to
true
- Type:boolean
- Specify if the hook should throw an exce...(QB_NEW_EN)
🤖 Prompt for AI Agents