File tree Expand file tree Collapse file tree 4 files changed +27
-9
lines changed Expand file tree Collapse file tree 4 files changed +27
-9
lines changed Original file line number Diff line number Diff line change @@ -56,15 +56,16 @@ yarn add react-router-prompt
56
56
}) => boolean
57
57
` ` `
58
58
59
- 2. ` beforeConfirm ()` : ` Promise < unknown> ` _(Optional)_
59
+ 2. ` beforeConfirm (nextLocation ?: Location )` : ` Promise < unknown> ` _(Optional)_
60
60
61
61
3. ` beforeCancel ()` : ` Promise < unknown> ` _(Optional)_
62
62
63
63
### Return values
64
64
65
65
1. ` isActive` : ` Boolean `
66
- 2. ` onConfirm ()` : ` void `
66
+ 2. ` onConfirm (nextLocation ?: Location )` : ` void `
67
67
3. ` onCancel ()` : ` void `
68
+ 4. ` nextLocation` : ` Location | undefined `
68
69
69
70
#### Note 🗒️
70
71
Original file line number Diff line number Diff line change 1
- import { BlockerFunction } from "react-router-dom"
1
+ import { BlockerFunction , Location } from "react-router-dom"
2
2
import usePrompt from "./use-prompt"
3
+ import { useState } from "react"
3
4
4
5
declare interface InitialStateType {
5
6
isActive : boolean
6
7
onConfirm ( ) : void
7
8
resetConfirmation ( ) : void
9
+ nextLocation ?: Location
8
10
}
9
11
10
12
const useConfirm = ( when : boolean | BlockerFunction ) : InitialStateType => {
11
- const blocker = usePrompt ( when )
13
+ const [ nextLocation , setNextLocation ] = useState < Location | null > ( null )
14
+
15
+ const blocker = usePrompt ( when , ( location ) => {
16
+ setNextLocation ( location )
17
+ } )
12
18
13
19
const resetConfirmation = ( ) => {
14
20
if ( blocker . state === "blocked" ) blocker . reset ( )
@@ -22,6 +28,7 @@ const useConfirm = (when: boolean | BlockerFunction): InitialStateType => {
22
28
isActive : blocker . state === "blocked" ,
23
29
onConfirm,
24
30
resetConfirmation,
31
+ nextLocation : nextLocation || undefined ,
25
32
}
26
33
}
27
34
Original file line number Diff line number Diff line change 4
4
useBlocker ,
5
5
Blocker ,
6
6
BlockerFunction ,
7
+ Location ,
7
8
} from "react-router-dom"
8
9
9
10
// You can abstract `useBlocker` to use the browser's `window.confirm` dialog to
@@ -17,13 +18,19 @@ import {
17
18
// or the app may stay on the correct page but the browser's history stack gets
18
19
// out of whack. You should test your own implementation thoroughly to make sure
19
20
// the tradeoffs are right for your users.
20
- function usePrompt ( when : boolean | BlockerFunction ) : Blocker {
21
+ function usePrompt (
22
+ when : boolean | BlockerFunction ,
23
+ onNavigate ?: ( nextLocation : Location ) => void ,
24
+ ) : Blocker {
21
25
const blocker = useBlocker ( when )
22
26
useEffect ( ( ) => {
27
+ if ( blocker . state === "blocked" && blocker . location && onNavigate ) {
28
+ onNavigate ( blocker . location )
29
+ }
23
30
if ( blocker . state === "blocked" && ! when ) {
24
31
blocker . reset ( )
25
32
}
26
- } , [ blocker , when ] )
33
+ } , [ blocker , when , onNavigate ] )
27
34
28
35
useBeforeUnload (
29
36
useCallback (
Original file line number Diff line number Diff line change 1
1
import React , { useCallback } from "react"
2
- import { BlockerFunction } from "react-router-dom"
2
+ import { BlockerFunction , Location } from "react-router-dom"
3
3
4
4
import useConfirm from "./hooks/use-confirm"
5
5
import usePrompt from "./hooks/use-prompt"
@@ -10,9 +10,10 @@ type ReactRouterPromptProps = {
10
10
isActive : boolean
11
11
onCancel : ( ) => void
12
12
onConfirm : ( ) => void
13
+ nextLocation ?: Location
13
14
} ) => React . ReactNode
14
15
beforeCancel ?: ( ) => Promise < unknown >
15
- beforeConfirm ?: ( ) => Promise < unknown >
16
+ beforeConfirm ?: ( nextLocation ?: Location ) => Promise < unknown >
16
17
}
17
18
18
19
/**
@@ -39,7 +40,8 @@ function ReactRouterPrompt({
39
40
beforeCancel,
40
41
beforeConfirm,
41
42
} : ReactRouterPromptProps ) {
42
- const { isActive, onConfirm, resetConfirmation } = useConfirm ( when )
43
+ const { isActive, onConfirm, resetConfirmation, nextLocation } =
44
+ useConfirm ( when )
43
45
44
46
const onConfirmAction = useCallback ( async ( ) => {
45
47
if ( beforeConfirm ) await beforeConfirm ( )
@@ -58,6 +60,7 @@ function ReactRouterPrompt({
58
60
isActive : true ,
59
61
onConfirm : onConfirmAction ,
60
62
onCancel : onResetAction ,
63
+ nextLocation : nextLocation || undefined ,
61
64
} ) }
62
65
</ >
63
66
)
You can’t perform that action at this time.
0 commit comments