Skip to content

docs: finish blocker doc #13803

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions docs/api/hooks/useBlocker.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Allow the application to block navigations within the SPA and present the user a
## Signature

```tsx
useBlocker(shouldBlock): Blocker
useBlocker(shouldBlock: boolean | BlockerFunction): Blocker
```

## Params
Expand All @@ -24,7 +24,43 @@ useBlocker(shouldBlock): Blocker

[modes: framework, data]

_No documentation_
**boolean**

Whether or not the navigation should be blocked. If `true`, the blocker will prevent the navigation. If `false`, the blocker will not prevent the navigation.

[**BlockerFunction**](https://api.reactrouter.com/v7/types/react_router.BlockerFunction.html)

A function that returns a boolean indicating whether the navigation should be blocked.

```tsx
const blocker = useBlocker(
({ currentLocation, nextLocation, historyAction }) =>
value !== "" &&
currentLocation.pathname !== nextLocation.pathname
);
```

## Blocker

The [Blocker](https://api.reactrouter.com/v7/types/react_router.Blocker.html) object returned by the hook. It has the following properties:

### `state`

- `unblocked` - the blocker is idle and has not prevented any navigation
- `blocked` - the blocker has prevented a navigation
- `proceeding` - the blocker is proceeding through from a blocked navigation

### `location`

When in a `blocked` state, this represents the location to which we blocked a navigation. When in a `proceeding` state, this is the location being navigated to after a `blocker.proceed()` call.

### `proceed()`

When in a `blocked` state, you may call `blocker.proceed()` to proceed to the blocked location.

### `reset()`

When in a `blocked` state, you may call `blocker.reset()` to return the blocker back to an `unblocked` state and leave the user at the current location.

## Examples

Expand Down