-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Stabilize useBlocker #7882
Merged
Merged
Stabilize useBlocker #7882
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3de446c
Stabilize useBlocker
brophdawg11 1028604
Point to RR experimental
brophdawg11 be52899
Point to RR experimental
brophdawg11 aad48c5
Merge branch 'dev' into brophdawg11/stabilize-useblocker
brophdawg11 44e0ad0
Yarn dedup
brophdawg11 eac8789
Bump router
brophdawg11 a59c1e7
Fix integration test
brophdawg11 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 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,5 @@ | ||
--- | ||
"@remix-run/react": minor | ||
--- | ||
|
||
Remove the `unstable_` prefix from the [`useBlocker`](https://reactrouter.com/en/main/hooks/use-blocker) hook as it's been in use for enough time that we are confident in the API. We do not plan to remove the prefix from `unstable_usePrompt` due to differences in how browsers handle `window.confirm` that prevent React Router from guaranteeing consistent/correct behavior. |
This file contains 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,82 @@ | ||
--- | ||
title: useBlocker | ||
--- | ||
|
||
# `useBlocker` | ||
|
||
The `useBlocker` hook allows you to prevent the user from navigating away from the current location, and present them with a custom UI to allow them to confirm the navigation. | ||
|
||
<docs-info> | ||
This only works for client-side navigations within your React Router application and will not block document requests. To prevent document navigations you will need to add your own <a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event" target="_blank">`beforeunload`</a> event handler. | ||
</docs-info> | ||
|
||
<docs-warning> | ||
Blocking a user from navigating is a bit of an anti-pattern, so please carefully consider any usage of this hook and use it sparingly. In the de-facto use case of preventing a user navigating away from a half-filled form, you might consider persisting unsaved state to `sessionStorage` and automatically re-filling it if they return instead of blocking them from navigating away. | ||
</docs-warning> | ||
|
||
```tsx | ||
function ImportantForm() { | ||
const [value, setValue] = React.useState(""); | ||
|
||
// Block navigating elsewhere when data has been entered into the input | ||
const blocker = useBlocker( | ||
({ currentLocation, nextLocation }) => | ||
value !== "" && | ||
currentLocation.pathname !== nextLocation.pathname | ||
); | ||
|
||
return ( | ||
<Form method="post"> | ||
<label> | ||
Enter some important data: | ||
<input | ||
name="data" | ||
value={value} | ||
onChange={(e) => setValue(e.target.value)} | ||
/> | ||
</label> | ||
<button type="submit">Save</button> | ||
|
||
{blocker.state === "blocked" ? ( | ||
<div> | ||
<p>Are you sure you want to leave?</p> | ||
<button onClick={() => blocker.proceed()}> | ||
Proceed | ||
</button> | ||
<button onClick={() => blocker.reset()}> | ||
Cancel | ||
</button> | ||
</div> | ||
) : null} | ||
</Form> | ||
); | ||
} | ||
``` | ||
|
||
For a more complete example, please refer to the [example][example] in the repository. | ||
|
||
## Properties | ||
|
||
### `state` | ||
|
||
The current state of the blocker | ||
|
||
- `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. | ||
|
||
## Methods | ||
|
||
### `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. | ||
|
||
[example]: https://github.com/remix-run/react-router/tree/main/examples/navigation-blocking |
This file contains 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,49 @@ | ||
--- | ||
title: unstable_usePrompt | ||
--- | ||
|
||
# `unstable_usePrompt` | ||
|
||
The `unstable_usePrompt` hook allows you to prompt the user for confirmation via [`window.confirm`][window-confirm] prior to navigating away from the current location. | ||
|
||
<docs-info> | ||
This only works for client-side navigations within your React Router application and will not block document requests. To prevent document navigations you will need to add your own <a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event" target="_blank">`beforeunload`</a> event handler. | ||
</docs-info> | ||
|
||
<docs-warning> | ||
Blocking a user from navigating is a bit of an anti-pattern, so please carefully consider any usage of this hook and use it sparingly. In the de-facto use case of preventing a user navigating away from a half-filled form, you might consider persisting unsaved state to `sessionStorage` and automatically re-filling it if they return instead of blocking them from navigating away. | ||
</docs-warning> | ||
|
||
<docs-warning> | ||
We do not plan to remove the `unstable_` prefix from this hook because the behavior is non-deterministic across browsers when the prompt is open, so React Router cannot guarantee correct behavior in all scenarios. To avoid this non-determinism, we recommend using `useBlocker` instead which also gives you control over the confirmation UX. | ||
</docs-warning> | ||
|
||
```tsx | ||
function ImportantForm() { | ||
const [value, setValue] = React.useState(""); | ||
|
||
// Block navigating elsewhere when data has been entered into the input | ||
unstable_usePrompt({ | ||
message: "Are you sure?", | ||
when: ({ currentLocation, nextLocation }) => | ||
value !== "" && | ||
currentLocation.pathname !== nextLocation.pathname, | ||
}); | ||
|
||
return ( | ||
<Form method="post"> | ||
<label> | ||
Enter some important data: | ||
<input | ||
name="data" | ||
value={value} | ||
onChange={(e) => setValue(e.target.value)} | ||
/> | ||
</label> | ||
<button type="submit">Save</button> | ||
</Form> | ||
); | ||
} | ||
``` | ||
|
||
[window-confirm]: https://developer.mozilla.org/en-US/docs/Web/API/Window/confirm |
This file contains 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 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 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 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 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 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 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.
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.
These were asserting the buggy behavior fixed by remix-run/react-router#10983