Skip to content

Commit 9762fad

Browse files
committed
docs: add blocker example
1 parent 26ecf2f commit 9762fad

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

docs/api/hooks/useBlocker.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,70 @@ useBlocker(shouldBlock): Blocker
2525
[modes: framework, data]
2626

2727
_No documentation_
28+
29+
## Examples
30+
31+
### Basic
32+
33+
```tsx
34+
import { useCallback, useState } from "react";
35+
import { BlockerFunction, useBlocker } from "react-router";
36+
37+
export function ImportantForm() {
38+
const [value, setValue] = useState("");
39+
40+
const shouldBlock = useCallback<BlockerFunction>(
41+
() => value !== "",
42+
[value]
43+
);
44+
const blocker = useBlocker(shouldBlock);
45+
46+
return (
47+
<form
48+
onSubmit={(e) => {
49+
e.preventDefault();
50+
setValue("");
51+
if (blocker.state === "blocked") {
52+
blocker.proceed();
53+
}
54+
}}
55+
>
56+
<input
57+
name="data"
58+
value={value}
59+
onChange={(e) => setValue(e.target.value)}
60+
/>
61+
62+
<button type="submit">Save</button>
63+
64+
{blocker.state === "blocked" ? (
65+
<>
66+
<p style={{ color: "red" }}>
67+
Blocked the last navigation to
68+
</p>
69+
<button
70+
type="button"
71+
onClick={() => blocker.proceed()}
72+
>
73+
Let me through
74+
</button>
75+
<button
76+
type="button"
77+
onClick={() => blocker.reset()}
78+
>
79+
Keep me here
80+
</button>
81+
</>
82+
) : blocker.state === "proceeding" ? (
83+
<p style={{ color: "orange" }}>
84+
Proceeding through blocked navigation
85+
</p>
86+
) : (
87+
<p style={{ color: "green" }}>
88+
Blocker is currently unblocked
89+
</p>
90+
)}
91+
</form>
92+
);
93+
}
94+
```

0 commit comments

Comments
 (0)