Skip to content

Commit

Permalink
docs(react-router): fix blocker doc
Browse files Browse the repository at this point in the history
  • Loading branch information
schiller-manuel committed Dec 15, 2024
1 parent e2004c1 commit 437fb74
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
8 changes: 4 additions & 4 deletions docs/framework/react/api/router/useBlockerHook.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ import { useBlocker } from '@tanstack/react-router'

function MyComponent() {
const { proceed, reset, status } = useBlocker({
shouldBlockFn: ({ nextLocation }) => {
return !nextLocation.pathname.includes('step/')
shouldBlockFn: ({ next }) => {
return !next.pathname.includes('step/')
},
withResolver: true,
})
Expand Down Expand Up @@ -169,8 +169,8 @@ function MyComponent() {
const [formIsDirty, setFormIsDirty] = useState(false)

useBlocker({
shouldBlockFn: ({ nextLocation }) => {
if (nextLocation.pathname.includes('step/')) {
shouldBlockFn: ({ next }) => {
if (next.pathname.includes('step/')) {
return false
}

Expand Down
27 changes: 26 additions & 1 deletion docs/framework/react/guide/navigation-blocking.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,29 @@ function MyComponent() {
}
```

`shouldBlockFn` gives you type safe access to the `current` and `next` location:

```tsx
import { useBlocker } from '@tanstack/react-router'

function MyComponent() {
// always block going from /foo to /bar/123?hello=world
const { proceed, reset, status } = useBlocker({
shouldBlockFn: ({ current, next }) => {
return (
current.routeId === '/foo' &&
next.fullPath === '/bar/$id' &&
next.params.id === 123 &&
next.search.hello === 'world'
)
},
withResolver: true,
})

// ...
}
```

You can find more information about the `useBlocker` hook in the [API reference](../api/router/useBlockerHook.md).

## Component-based blocking
Expand Down Expand Up @@ -132,7 +155,9 @@ function MyComponent() {

useBlocker({
shouldBlockFn: () => {
if (!formIsDirty) return false
if (!formIsDirty) {
return false
}

const shouldLeave = new Promise<boolean>((resolve) => {
// Using a modal manager of your choice
Expand Down

0 comments on commit 437fb74

Please sign in to comment.