Skip to content

Commit b396d38

Browse files
committed
Start on Transitions page
1 parent 76dd3dd commit b396d38

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

docs/Glossary.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ The term *route component* refers to a [component](#component) that is directly
136136

137137
A *route config* is an array of [route](#route)s that specifies the order in which routes should be tried when the router attempts to match a URL.
138138

139+
### RouteHook
140+
141+
type RouteHook = (nextLocation?: Location) => any;
142+
143+
A *route hook* is a function that is used to prevent the user from leaving a route. On normal transitions, it receives the next [location](#location) as an argument and must either `return false` to cancel the transition or `return` a prompt message to show the user. When invoked during the `beforeunload` event in web browsers, it does not receive any arguments and must `return` a prompt message to cancel the transition.
144+
139145
### RoutePattern
140146

141147
type RoutePattern = string;

docs/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
- [Introduction](Introduction.md)
44
- [Route Configuration](RouteConfiguration.md)
55
- [Route Matching](RouteMatching.md)
6-
- [Dynamic Routing](DynamicRouting.md)
7-
- [Advanced Usage](AdvancedUsage.md)
8-
- [Server Rendering](ServerRendering.md)
96
- [Transitions](Transitions.md)
107
- [Glossary](Glossary.md)
8+
9+
- [Dynamic Routing](DynamicRouting.md)
10+
- [Server Rendering](ServerRendering.md)

docs/Transitions.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
## Transitions
2+
3+
TODO: talk about onEnter/onLeave
4+
5+
### Lifecycle Method
6+
7+
React Router provides a [`routerWillLeave` lifecycle hook](Glossary.md#routehook) that React [component](Glossary.md#component)s may use to prevent the user from leaving a [route](Glossary.md#route) by `return`ing either `false` or a prompt message.
8+
9+
To install this hook, use the `Lifecycle` mixin either directly in one of your [route component](Glossary.md#routecomponent)s or in a component further down the hierarchy whose [route component](Glossary.md#routecomponent) uses the `RouteContext` mixin.
10+
11+
```js
12+
import { Lifecycle, RouteContext } from 'react-router';
13+
14+
// Assuming Home is a route component, it may use the
15+
// Lifecycle mixin directly.
16+
var Home = React.createClass({
17+
18+
mixins: [ Lifecycle ],
19+
20+
routerWillLeave(nextLocation) {
21+
if (!this.state.isSaved)
22+
return 'Your work is not saved! Are you sure you want to leave?';
23+
},
24+
25+
// ...
26+
27+
});
28+
29+
// Otherwise, Home should provide its route in context
30+
// for descendants further down the hierarchy.
31+
var Home = React.createClass({
32+
33+
mixins: [ RouteContext ],
34+
35+
render() {
36+
return <NestedForm />;
37+
}
38+
39+
});
40+
41+
var NestedForm = React.createClass({
42+
43+
mixins: [ Lifecycle ],
44+
45+
routerWillLeave(nextLocation) {
46+
if (!this.state.isSaved)
47+
return 'Your work is not saved! Are you sure you want to leave?';
48+
},
49+
50+
// ...
51+
52+
});
53+
```

0 commit comments

Comments
 (0)