Skip to content

Commit 8ef625e

Browse files
committed
Use componentDidMount in Lifecycle mixin
1 parent 9e37b03 commit 8ef625e

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

modules/Lifecycle.js

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,23 @@ import invariant from 'invariant'
33

44
const { object } = React.PropTypes
55

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-
186
/**
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.
2210
*
2311
* On standard transitions, routerWillLeave receives a single argument: the
2412
* location we're transitioning to. To cancel the transition, return false.
2513
* To prompt the user for confirmation, return a prompt message (string).
2614
*
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.
3220
*/
3321
const Lifecycle = {
3422

35-
propTypes: {
36-
// Route components receive the route object as a prop.
37-
route: object
38-
},
39-
4023
contextTypes: {
4124
history: object.isRequired,
4225
// Nested children receive the route as context, either
@@ -45,14 +28,27 @@ const Lifecycle = {
4528
route: object
4629
},
4730

48-
componentWillMount() {
31+
propTypes: {
32+
// Route components receive the route object as a prop.
33+
route: object
34+
},
35+
36+
componentDidMount() {
4937
invariant(
5038
this.routerWillLeave,
5139
'The Lifecycle mixin requires you to define a routerWillLeave method'
5240
)
5341

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+
5450
this._unlistenBeforeLeavingRoute = this.context.history.listenBeforeLeavingRoute(
55-
getRoute(this),
51+
route,
5652
this.routerWillLeave
5753
)
5854
},

0 commit comments

Comments
 (0)