1
1
import React from "react" ;
2
2
import PropTypes from "prop-types" ;
3
3
import invariant from "invariant" ;
4
+ import RouterContext from "./RouterContext" ;
4
5
5
6
/**
6
7
* The public API for prompting the user before navigating away
7
8
* from a screen with a component.
8
9
*/
9
- class Prompt extends React . Component {
10
+ class InnerPrompt extends React . Component {
10
11
static propTypes = {
11
12
when : PropTypes . bool ,
12
- message : PropTypes . oneOfType ( [ PropTypes . func , PropTypes . string ] ) . isRequired
13
- } ;
14
-
15
- static defaultProps = {
16
- when : true
17
- } ;
18
-
19
- static contextTypes = {
13
+ message : PropTypes . oneOfType ( [ PropTypes . func , PropTypes . string ] ) . isRequired ,
20
14
router : PropTypes . shape ( {
21
15
history : PropTypes . shape ( {
22
16
block : PropTypes . func . isRequired
23
17
} ) . isRequired
24
18
} ) . isRequired
25
19
} ;
26
20
21
+ static defaultProps = {
22
+ when : true
23
+ } ;
24
+
27
25
enable ( message ) {
28
26
if ( this . unblock ) this . unblock ( ) ;
29
27
30
- this . unblock = this . context . router . history . block ( message ) ;
28
+ this . unblock = this . props . router . history . block ( message ) ;
31
29
}
32
30
33
31
disable ( ) {
@@ -39,7 +37,7 @@ class Prompt extends React.Component {
39
37
40
38
componentWillMount ( ) {
41
39
invariant (
42
- this . context . router ,
40
+ this . props . router ,
43
41
"You should not use <Prompt> outside a <Router>"
44
42
) ;
45
43
@@ -64,4 +62,9 @@ class Prompt extends React.Component {
64
62
}
65
63
}
66
64
65
+ const Prompt = props =>
66
+ RouterContext . consume ( ( { router } ) => (
67
+ < InnerPrompt { ...props } router = { router } />
68
+ ) ) ;
69
+
67
70
export default Prompt ;
0 commit comments