@@ -3,39 +3,64 @@ React Router
3
3
4
4
Declarative routing with automatic UI nesting for React.
5
5
6
- Thanks, Ember
7
- -------------
6
+ Features
7
+ --------
8
+
9
+ - Nested views mapped to nested routes
10
+ - Dynamic segments
11
+ - Query parameters
12
+ - Links
13
+ - Transition abort / retry
14
+ - Multiple root routes
15
+ - hash or history urls
16
+
17
+ Check out the examples directory to see how simple previously complex UI
18
+ and workflows are to create.
19
+
20
+ Installation
21
+ ------------
8
22
9
- This library is highly inspired by the Ember.js routing API. Huge thanks to
10
- the Ember team.
23
+ ` npm install rpflorence/react-router `
24
+
25
+ We'll get on npm's registry soon.
11
26
12
27
WIP
13
28
---
14
29
15
- This is a huge work in progress. Don't use it yet .
30
+ This is a work in progress but the API seems to have settled .
16
31
17
- Usage Example
18
- -------------
32
+ Usage
33
+ -----
19
34
20
35
``` js
36
+ var Router = require (' react-router' );
37
+ var Route = Router .Route ;
38
+ var Link = Router .Link ;
39
+
21
40
Router (
22
41
< Route handler= {App}>
23
- < Route name= " about" path = " about " handler= {About}/ >
24
- < Route name= " users" path = " users " handler= {Users}>
42
+ < Route name= " about" handler= {About}/ >
43
+ < Route name= " users" handler= {Users}>
25
44
< Route name= " user" path= " user/:userId" handler= {User}/ >
26
45
< / Route>
27
46
< / Route>
28
47
).renderComponent (document .body );
29
48
```
30
49
31
- The rest of the app ...
50
+ When a ` Route ` is active, you'll get an instance of ` handler `
51
+ automatically rendered for you. When one of the child routes is active,
52
+ you can render it with ` this.props.activeRoute ` all the way down the
53
+ view hierarchy. This allows you to create nested layouts without having
54
+ to wire it all up yourself. ` Link ` components create accessible anchor
55
+ tags to route you around the application.
56
+
57
+ Here's the rest of the application:
32
58
33
59
``` js
34
60
var App = React .createClass ({
35
61
render : function () {
36
62
return (
37
- < div className= " App" >
38
- < h1> App< / h1>
63
+ < div>
39
64
< ul>
40
65
< li>< Link to= " about" > About< / Link>< / li>
41
66
< li>< Link to= " users" > Users< / Link>< / li>
@@ -56,7 +81,7 @@ var About = React.createClass({
56
81
var Users = React .createClass ({
57
82
render : function () {
58
83
return (
59
- < div className = " Users " >
84
+ < div>
60
85
< h2> Users< / h2>
61
86
{this .props .activeRoute }
62
87
< / div>
@@ -167,6 +192,73 @@ Route({handler: App},
167
192
168
193
Paths are not inherited from parent routes.
169
194
195
+ ### Handlers
196
+
197
+ There are some properties and hooks available to the handlers you pass
198
+ to the ` handler ` property of a ` Route ` .
199
+
200
+ #### Properties
201
+
202
+ ** this.props.activeRoute** - The active child route handler instance.
203
+ Use it in your render method to render the child route.
204
+
205
+ ** this.props.params** - When a route has dynamic segments like `<Route
206
+ path="users/: userId "/>` the dynamic values are available at
207
+ ` this.props.params.userId ` , etc.
208
+
209
+ ** this.props.query** - The query parameters from the url.
210
+
211
+ ### Transition Hooks
212
+
213
+ You can define static methods on your route handlers that will be called
214
+ during route transitions.
215
+
216
+ ** willTransitionTo(transition, params)** - Called when a route is about
217
+ to render, giving you the opportunity to abort the transition. You can
218
+ return a promise and the whole route hierarchy will wait for the
219
+ promises to resolve before proceeding. This is especially useful for
220
+ server-side rendering when you need to populate some data before the
221
+ handler is rendered.
222
+
223
+ ** willTransitionFrom(transition, component)** - Called when an active
224
+ route is being transitioned out giving you an opportunity to abort the
225
+ transition. The ` component ` is the current component, you'll probably
226
+ need it to check its state to decide if you want to allow the
227
+ transition.
228
+
229
+ #### transition (object)
230
+
231
+ ##### transition.abort() - aborts a transition
232
+
233
+ ##### transition.retry() - retrys a transition
234
+
235
+ #### Example
236
+
237
+ ``` js
238
+ var Settings = React .createClass ({
239
+ statics: {
240
+ willTransitionTo : function (transition , params ) {
241
+ return auth .isLoggedIn ().then (function (loggedIn ) {
242
+ if (! loggedIn)
243
+ return ;
244
+ transition .abort ();
245
+ return auth .logIn ({transition: transition});
246
+ // in auth module call `transition.retry()` after being logged in
247
+ });
248
+ },
249
+
250
+ willTransitionFrom : function (transition , component ) {
251
+ if (component .formHasUnsavedData ())) {
252
+ if (! confirm (' You have unsaved information, are you sure you want to leave this page?' )) {
253
+ transition .abort ();
254
+ }
255
+ }
256
+ }
257
+ }
258
+
259
+ // ...
260
+ });
261
+ ```
170
262
171
263
### Link (Component)
172
264
@@ -225,3 +317,10 @@ Development
225
317
` script/test ` will fire up a karma runner, ` npm test ` will do the same
226
318
but with the ` --single-run ` option.
227
319
320
+ Thanks, Ember
321
+ -------------
322
+
323
+ This library is highly inspired by the Ember.js routing API. In general,
324
+ its a translation of the Ember router api to React. Huge thanks to the
325
+ Ember team for solving the hardest part already.
326
+
0 commit comments