Skip to content

Commit bf6f5f4

Browse files
committed
addRouteLeaveHook -> setRouteLeaveHook
1 parent 1b52945 commit bf6f5f4

File tree

8 files changed

+19
-12
lines changed

8 files changed

+19
-12
lines changed

CHANGES.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ const RouteComponent = React.createClass({
131131
const RouteComponent = React.createClass({
132132
componentDidMount() {
133133
const { router, route } = this.props
134-
router.addRouteLeaveHook(route, this.routerWillLeave)
134+
router.setRouteLeaveHook(route, this.routerWillLeave)
135135
}
136136
})
137137

@@ -171,7 +171,7 @@ const DeepComponent = React.createClass({
171171

172172
componentDidMount() {
173173
const { router, route } = this.context
174-
router.addRouteLeaveHook(route, this.routerWillLeave)
174+
router.setRouteLeaveHook(route, this.routerWillLeave)
175175
}
176176
})
177177

@@ -187,7 +187,7 @@ const Lifecycle = {
187187
componentDidMount() {
188188
const router = this.context.router
189189
const route = this.props.route || this.context.route
190-
router.addRouteLeaveHook(route, this.routerWillLeave)
190+
router.setRouteLeaveHook(route, this.routerWillLeave)
191191
}
192192
}
193193
```

examples/transitions/app.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const Dashboard = React.createClass({
2525
const Form = React.createClass({
2626

2727
componentWillMount() {
28-
this.props.router.addRouteLeaveHook(
28+
this.props.router.setRouteLeaveHook(
2929
this.props.route,
3030
this.routerWillLeave
3131
)

modules/Lifecycle.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const Lifecycle = {
3535
},
3636

3737
componentDidMount() {
38-
warning(false, 'the `Lifecycle` mixin is deprecated, please use `this.props.router.addRouteLeaveHook(this.props.route, hook)` from a route component, see https://github.com/rackt/react-router/blob/v1.1.0/CHANGES.md#v110 for more details.')
38+
warning(false, 'the `Lifecycle` mixin is deprecated, please use `this.props.router.setRouteLeaveHook(this.props.route, hook)` from a route component, see https://github.com/rackt/react-router/blob/v1.1.0/CHANGES.md#v110 for more details.')
3939
invariant(
4040
this.routerWillLeave,
4141
'The Lifecycle mixin requires you to define a routerWillLeave method'

modules/RouterContext.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const RouterContext = React.createClass({
4343

4444
router = {
4545
...history,
46-
addRouteLeaveHook: history.listenBeforeLeavingRoute
46+
setRouteLeaveHook: history.listenBeforeLeavingRoute
4747
}
4848
delete router.listenBeforeLeavingRoute
4949
}

modules/RouterUtils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import deprecateObjectProperties from './deprecateObjectProperties'
33
export function createRouterObject(history, transitionManager) {
44
return {
55
...history,
6-
addRouteLeaveHook: transitionManager.listenBeforeLeavingRoute,
6+
setRouteLeaveHook: transitionManager.listenBeforeLeavingRoute,
77
isActive: transitionManager.isActive
88
}
99
}

modules/__tests__/RouterContext-test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ describe('RouterContext', () => {
8686
})
8787
})
8888

89-
it('proxies calls to `addRouteLeaveHook` to `props.transitionManager`', (done) => {
89+
it('proxies calls to `setRouteLeaveHook` to `props.transitionManager`', (done) => {
9090
const args = [ 1, 2, 3 ]
9191
renderTest(() => {
92-
const remove = context.router.addRouteLeaveHook(...args)
92+
const remove = context.router.setRouteLeaveHook(...args)
9393
expect(transitionManager.listenBeforeLeavingRoute).toHaveBeenCalledWith(...args)
9494
expect(remove).toBe(listenBeforeLeavingRouteSentinel)
9595
done()

modules/__tests__/transitionHooks-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('When a router enters a branch', function () {
2929

3030
class NewsFeed extends Component {
3131
componentWillMount() {
32-
removeLeaveHook = this.context.router.addRouteLeaveHook(
32+
removeLeaveHook = this.context.router.setRouteLeaveHook(
3333
this.props.route,
3434
() => leaveHookSpy() // Break reference equality.
3535
)

modules/createTransitionManager.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,15 @@ export default function createTransitionManager(history, routes) {
218218
if (history.listenBeforeUnload)
219219
unlistenBeforeUnload = history.listenBeforeUnload(beforeUnloadHook)
220220
}
221-
} else if (hooks.indexOf(hook) === -1) {
222-
hooks.push(hook)
221+
} else {
222+
warning(
223+
false,
224+
'adding multiple leave hooks for the same route is deprecated; manage multiple confirmations in your own code instead'
225+
)
226+
227+
if (hooks.indexOf(hook) === -1) {
228+
hooks.push(hook)
229+
}
223230
}
224231

225232
return function () {

0 commit comments

Comments
 (0)