Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prevState to onLeave hooks #3616

Merged
merged 2 commits into from
Jul 7, 2016
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