|
| 1 | +--- |
| 2 | +title: useBlocker |
| 3 | +--- |
| 4 | + |
| 5 | +# `useBlocker` |
| 6 | + |
| 7 | +<details> |
| 8 | + <summary>Type declaration</summary> |
| 9 | + |
| 10 | +```tsx |
| 11 | +declare function useBlocker( |
| 12 | + shouldBlock: boolean | BlockerFunction |
| 13 | +): Blocker; |
| 14 | + |
| 15 | +type BlockerFunction = (args: { |
| 16 | + currentLocation: Location; |
| 17 | + nextLocation: Location; |
| 18 | + historyAction: HistoryAction; |
| 19 | +}) => boolean; |
| 20 | + |
| 21 | +type Blocker = |
| 22 | + | { |
| 23 | + state: "unblocked"; |
| 24 | + reset: undefined; |
| 25 | + proceed: undefined; |
| 26 | + location: undefined; |
| 27 | + } |
| 28 | + | { |
| 29 | + state: "blocked"; |
| 30 | + reset(): void; |
| 31 | + proceed(): void; |
| 32 | + location: Location; |
| 33 | + } |
| 34 | + | { |
| 35 | + state: "proceeding"; |
| 36 | + reset: undefined; |
| 37 | + proceed: undefined; |
| 38 | + location: Location; |
| 39 | + }; |
| 40 | + |
| 41 | +interface Location<State = any> extends Path { |
| 42 | + state: State; |
| 43 | + key: string; |
| 44 | +} |
| 45 | + |
| 46 | +interface Path { |
| 47 | + pathname: string; |
| 48 | + search: string; |
| 49 | + hash: string; |
| 50 | +} |
| 51 | + |
| 52 | +enum HistoryAction { |
| 53 | + Pop = "POP", |
| 54 | + Push = "PUSH", |
| 55 | + Replace = "REPLACE", |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +</details> |
| 60 | + |
| 61 | +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. |
| 62 | + |
| 63 | +<docs-info> |
| 64 | +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. |
| 65 | +</docs-info> |
| 66 | + |
| 67 | +<docs-warning> |
| 68 | +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. |
| 69 | +</docs-warning> |
| 70 | + |
| 71 | +```tsx |
| 72 | +function ImportantForm() { |
| 73 | + let [value, setValue] = React.useState(""); |
| 74 | + |
| 75 | + // Block navigating elsewhere when data has been entered into the input |
| 76 | + let blocker = useBlocker( |
| 77 | + ({ currentLocation, nextLocation }) => |
| 78 | + value !== "" && |
| 79 | + currentLocation.pathname !== nextLocation.pathname |
| 80 | + ); |
| 81 | + |
| 82 | + return ( |
| 83 | + <Form method="post"> |
| 84 | + <label> |
| 85 | + Enter some important data: |
| 86 | + <input |
| 87 | + name="data" |
| 88 | + value={value} |
| 89 | + onChange={(e) => setValue(e.target.value)} |
| 90 | + /> |
| 91 | + </label> |
| 92 | + <button type="submit">Save</button> |
| 93 | + |
| 94 | + {blocker.state === "blocked" ? ( |
| 95 | + <div> |
| 96 | + <p>Are you sure you want to leave?</p> |
| 97 | + <button onClick={() => blocker.proceed()}> |
| 98 | + Proceed |
| 99 | + </button> |
| 100 | + <button onClick={() => blocker.reset()}> |
| 101 | + Cancel |
| 102 | + </button> |
| 103 | + </div> |
| 104 | + ) : null} |
| 105 | + </Form> |
| 106 | + ); |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +For a more complete example, please refer to the [example][example] in the repository. |
| 111 | + |
| 112 | +## Properties |
| 113 | + |
| 114 | +### `state` |
| 115 | + |
| 116 | +The current state of the blocker |
| 117 | + |
| 118 | +- `unblocked` - the blocker is idle and has not prevented any navigation |
| 119 | +- `blocked` - the blocker has prevented a navigation |
| 120 | +- `proceeding` - the blocker is proceeding through from a blocked navigation |
| 121 | + |
| 122 | +### `location` |
| 123 | + |
| 124 | +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. |
| 125 | + |
| 126 | +## Methods |
| 127 | + |
| 128 | +### `proceed()` |
| 129 | + |
| 130 | +When in a `blocked` state, you may call `blocker.proceed()` to proceed to the blocked location. |
| 131 | + |
| 132 | +### `reset()` |
| 133 | + |
| 134 | +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. |
| 135 | + |
| 136 | +[example]: https://github.com/remix-run/react-router/tree/main/examples/navigation-blocking |
0 commit comments