Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ Called on routes when the location changes, but the route itself neither enters

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

##### `onLeave()`
##### `onLeave(prevState)`
Called when a route is about to be exited.


Expand Down
4 changes: 2 additions & 2 deletions docs/Glossary.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ A *hash* is a string that represents the hash portion of the URL. It is synonymo
## LeaveHook

```js
type LeaveHook = () => any;
type LeaveHook = (prevState: RouterState) => any;
```

A *leave hook* is a user-defined function that is called when a route is about to be unmounted.
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.

## Location

Expand Down
4 changes: 2 additions & 2 deletions modules/TransitionUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ export function runChangeHooks(routes, state, nextState, callback) {
/**
* Runs all onLeave hooks in the given array of routes in order.
*/
export function runLeaveHooks(routes) {
export function runLeaveHooks(routes, prevState) {
for (let i = 0, len = routes.length; i < len; ++i)
if (routes[i].onLeave)
routes[i].onLeave.call(routes[i])
routes[i].onLeave.call(routes[i], prevState)
}
15 changes: 10 additions & 5 deletions modules/__tests__/transitionHooks-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ describe('When a router enters a branch', function () {
expect(nextState.routes).toContain(NewsFeedRoute)
expect(replace).toBeA('function')
},
onLeave() {
onLeave(prevState) {
expect(this).toBe(NewsFeedRoute)
expect(prevState.routes).toContain(NewsFeedRoute)
}
}

Expand All @@ -103,8 +104,9 @@ describe('When a router enters a branch', function () {
expect(nextState.routes).toContain(InboxRoute)
expect(replace).toBeA('function')
},
onLeave() {
onLeave(prevState) {
expect(this).toBe(InboxRoute)
expect(prevState.routes).toContain(InboxRoute)
}
}

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

replace('/inbox')
},
onLeave() {
onLeave(prevState) {
expect(this).toBe(RedirectToInboxRoute)
expect(prevState.routes).toContain(RedirectToInboxRoute)
}
}

Expand All @@ -135,8 +138,9 @@ describe('When a router enters a branch', function () {
expect(nextState.routes).toContain(MessageRoute)
expect(replace).toBeA('function')
},
onLeave() {
onLeave(prevState) {
expect(this).toBe(MessageRoute)
expect(prevState.routes).toContain(MessageRoute)
}
}

Expand Down Expand Up @@ -170,8 +174,9 @@ describe('When a router enters a branch', function () {
expect(nextState.routes).toContain(DashboardRoute)
expect(replace).toBeA('function')
},
onLeave() {
onLeave(prevState) {
expect(this).toBe(DashboardRoute)
expect(prevState.routes).toContain(DashboardRoute)
},
childRoutes: [ NewsFeedRoute, InboxRoute, RedirectToInboxRoute, MessageRoute, UserRoute ]
}
Expand Down
2 changes: 1 addition & 1 deletion modules/createTransitionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default function createTransitionManager(history, routes) {
function finishMatch(nextState, callback) {
const { leaveRoutes, changeRoutes, enterRoutes } = computeChangedRoutes(state, nextState)

runLeaveHooks(leaveRoutes)
runLeaveHooks(leaveRoutes, state)

// Tear down confirmation hooks for left routes
leaveRoutes
Expand Down