Skip to content

Commit 112cce4

Browse files
authored
Add prevState to onLeave hooks (#3616)
* Add prevState to leave hook args. * Update docs. Fill in some missed tests.
1 parent cd6031e commit 112cce4

File tree

5 files changed

+16
-11
lines changed

5 files changed

+16
-11
lines changed

docs/API.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ Called on routes when the location changes, but the route itself neither enters
350350

351351
If `callback` is listed as a 4th argument, this hook will run asynchronously, and the transition will block until `callback` is called.
352352

353-
##### `onLeave()`
353+
##### `onLeave(prevState)`
354354
Called when a route is about to be exited.
355355

356356

docs/Glossary.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ A *hash* is a string that represents the hash portion of the URL. It is synonymo
6666
## LeaveHook
6767

6868
```js
69-
type LeaveHook = () => any;
69+
type LeaveHook = (prevState: RouterState) => any;
7070
```
7171

72-
A *leave hook* is a user-defined function that is called when a route is about to be unmounted.
72+
A *leave hook* is a user-defined function that is called when a route is about to be unmounted. It receives the previous [router state](#routerstate) as its first argument.
7373

7474
## Location
7575

modules/TransitionUtils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ export function runChangeHooks(routes, state, nextState, callback) {
104104
/**
105105
* Runs all onLeave hooks in the given array of routes in order.
106106
*/
107-
export function runLeaveHooks(routes) {
107+
export function runLeaveHooks(routes, prevState) {
108108
for (let i = 0, len = routes.length; i < len; ++i)
109109
if (routes[i].onLeave)
110-
routes[i].onLeave.call(routes[i])
110+
routes[i].onLeave.call(routes[i], prevState)
111111
}

modules/__tests__/transitionHooks-test.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ describe('When a router enters a branch', function () {
9090
expect(nextState.routes).toContain(NewsFeedRoute)
9191
expect(replace).toBeA('function')
9292
},
93-
onLeave() {
93+
onLeave(prevState) {
9494
expect(this).toBe(NewsFeedRoute)
95+
expect(prevState.routes).toContain(NewsFeedRoute)
9596
}
9697
}
9798

@@ -103,8 +104,9 @@ describe('When a router enters a branch', function () {
103104
expect(nextState.routes).toContain(InboxRoute)
104105
expect(replace).toBeA('function')
105106
},
106-
onLeave() {
107+
onLeave(prevState) {
107108
expect(this).toBe(InboxRoute)
109+
expect(prevState.routes).toContain(InboxRoute)
108110
}
109111
}
110112

@@ -117,8 +119,9 @@ describe('When a router enters a branch', function () {
117119

118120
replace('/inbox')
119121
},
120-
onLeave() {
122+
onLeave(prevState) {
121123
expect(this).toBe(RedirectToInboxRoute)
124+
expect(prevState.routes).toContain(RedirectToInboxRoute)
122125
}
123126
}
124127

@@ -135,8 +138,9 @@ describe('When a router enters a branch', function () {
135138
expect(nextState.routes).toContain(MessageRoute)
136139
expect(replace).toBeA('function')
137140
},
138-
onLeave() {
141+
onLeave(prevState) {
139142
expect(this).toBe(MessageRoute)
143+
expect(prevState.routes).toContain(MessageRoute)
140144
}
141145
}
142146

@@ -170,8 +174,9 @@ describe('When a router enters a branch', function () {
170174
expect(nextState.routes).toContain(DashboardRoute)
171175
expect(replace).toBeA('function')
172176
},
173-
onLeave() {
177+
onLeave(prevState) {
174178
expect(this).toBe(DashboardRoute)
179+
expect(prevState.routes).toContain(DashboardRoute)
175180
},
176181
childRoutes: [ NewsFeedRoute, InboxRoute, RedirectToInboxRoute, MessageRoute, UserRoute ]
177182
}

modules/createTransitionManager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export default function createTransitionManager(history, routes) {
6969
function finishMatch(nextState, callback) {
7070
const { leaveRoutes, changeRoutes, enterRoutes } = computeChangedRoutes(state, nextState)
7171

72-
runLeaveHooks(leaveRoutes)
72+
runLeaveHooks(leaveRoutes, state)
7373

7474
// Tear down confirmation hooks for left routes
7575
leaveRoutes

0 commit comments

Comments
 (0)