@@ -3,40 +3,23 @@ import invariant from 'invariant'
3
3
4
4
const { object } = React . PropTypes
5
5
6
- function getRoute ( element ) {
7
- const route = element . props . route || element . context . route
8
-
9
- invariant (
10
- route ,
11
- 'The Lifecycle mixin needs to be used either on 1) a <Route component> or ' +
12
- '2) a descendant of a <Route component> that uses the RouteContext mixin'
13
- )
14
-
15
- return route
16
- }
17
-
18
6
/**
19
- * The Lifecycle mixin adds the routerWillLeave lifecycle method
20
- * to a component that may be used to cancel a transition or prompt
21
- * the user for confirmation.
7
+ * The Lifecycle mixin adds the routerWillLeave lifecycle method to a
8
+ * component that may be used to cancel a transition or prompt the user
9
+ * for confirmation.
22
10
*
23
11
* On standard transitions, routerWillLeave receives a single argument: the
24
12
* location we're transitioning to. To cancel the transition, return false.
25
13
* To prompt the user for confirmation, return a prompt message (string).
26
14
*
27
- * routerWillLeave does not receive a location object during the beforeunload
28
- * event in web browsers (assuming you're using the useBeforeUnload history
29
- * enhancer). In this case, it is not possible for us to know the location
30
- * we're transitioning to so routerWillLeave must return a prompt message to
31
- * prevent the user from closing the tab.
15
+ * During the beforeunload event (assuming you're using the useBeforeUnload
16
+ * history enhancer), routerWillLeave does not receive a location object
17
+ * because it isn't possible for us to know the location we're transitioning
18
+ * to. In this case routerWillLeave must return a prompt message to prevent
19
+ * the user from closing the window/ tab.
32
20
*/
33
21
const Lifecycle = {
34
22
35
- propTypes : {
36
- // Route components receive the route object as a prop.
37
- route : object
38
- } ,
39
-
40
23
contextTypes : {
41
24
history : object . isRequired ,
42
25
// Nested children receive the route as context, either
@@ -45,14 +28,27 @@ const Lifecycle = {
45
28
route : object
46
29
} ,
47
30
48
- componentWillMount ( ) {
31
+ propTypes : {
32
+ // Route components receive the route object as a prop.
33
+ route : object
34
+ } ,
35
+
36
+ componentDidMount ( ) {
49
37
invariant (
50
38
this . routerWillLeave ,
51
39
'The Lifecycle mixin requires you to define a routerWillLeave method'
52
40
)
53
41
42
+ const route = this . props . route || this . context . route
43
+
44
+ invariant (
45
+ route ,
46
+ 'The Lifecycle mixin must be used on either a) a <Route component> or ' +
47
+ 'b) a descendant of a <Route component> that uses the RouteContext mixin'
48
+ )
49
+
54
50
this . _unlistenBeforeLeavingRoute = this . context . history . listenBeforeLeavingRoute (
55
- getRoute ( this ) ,
51
+ route ,
56
52
this . routerWillLeave
57
53
)
58
54
} ,
0 commit comments