@@ -9,12 +9,14 @@ Features
9
9
--------
10
10
11
11
- Nested views mapped to nested routes
12
+ - Modular construction of route hierarchy
13
+ - Fully asynchronous transition hooks
14
+ - Transition abort / redirect / retry
12
15
- Dynamic segments
13
16
- Query parameters
14
17
- Links with automatic ` .active ` class when their route is active
15
- - Transition abort / retry
16
18
- Multiple root routes
17
- - hash or history urls
19
+ - Hash or HTML5 history URLs
18
20
19
21
Check out the ` examples ` directory to see how simple previously complex UI
20
22
and workflows are to create.
@@ -27,38 +29,33 @@ Installation
27
29
Usage
28
30
-----
29
31
30
- This library only ships with common js modules, so you'll need browserify or
32
+ This library only ships with CommonJS modules, so you'll need browserify or
31
33
webpack or something that can load/bundle it.
32
34
33
35
```
34
- var ReactRouter = require('react-nested-router');
35
- var Router = ReactRouter.Router;
36
- var Route = ReactRouter.Route;
37
- var Link = ReactRouter.Link;
36
+ var Route = require('react-nested-router').Route;
38
37
39
- // note that you always need a root route like `App` here
40
-
41
- Router(
38
+ React.renderComponent((
42
39
<Route handler={App}>
43
40
<Route name="about" handler={About}/>
44
41
<Route name="users" handler={Users}>
45
- <Route name="user" path="user/:userId" handler={User}/>
42
+ <Route name="user" path="/ user/:userId" handler={User}/>
46
43
</Route>
47
44
</Route>
48
- ).renderComponent( document.body);
45
+ ), document.body);
49
46
```
50
47
51
48
Or if JSX isn't your jam:
52
49
53
50
``` js
54
- Router (
51
+ React . renderComponent ( (
55
52
Route ({handler: App},
56
53
Route ({name: " about" , handler: About}),
57
54
Route ({name: " users" , handler: Users},
58
- Route ({name= " user" , path: " user/:userId" , handler: User})
55
+ Route ({name: " user" , path: " / user/:userId" , handler: User})
59
56
)
60
57
)
61
- ). renderComponent ( document .body );
58
+ ), document .body );
62
59
```
63
60
64
61
When a ` Route ` is active, you'll get an instance of ` handler `
@@ -71,6 +68,8 @@ accessible anchor tags to route you around the application.
71
68
Here's the rest of the application:
72
69
73
70
``` js
71
+ var Link = require (' react-nested-router' ).Link ;
72
+
74
73
var App = React .createClass ({
75
74
render : function () {
76
75
return (
@@ -138,62 +137,24 @@ Benefits of This Approach
138
137
API
139
138
---
140
139
141
- ### Router (Constructor)
142
-
143
- Router config constructor.
144
-
145
- ``` jsx
146
- Router (
147
- < Route handler= {App}>
148
- < Route path= " /users" name= " userList" handler= {UserList}>
149
- < Route path= " /users/:id" name= " user" handler= {User}/ >
150
- < / Route>
151
- < / Route>
152
- );
153
- ```
154
-
155
- #### Methods
156
-
157
- ** renderComponent(Node)** - Renders the top level handler into ` Node ` .
158
-
159
- ``` js
160
- var router = Router (routes);
161
- router .renderComponent (document .body ):
162
- ```
163
-
164
- #### Constructor Methods
165
-
166
- ** useHistory()** - Uses the browser's history API instead of hashes.
167
-
168
- ``` js
169
- Router .useHistory ();
170
- ```
171
-
172
- ** transitionTo(routeName, [ params[ , query]] )** - Programatically transition to a new route.
173
-
174
- ``` js
175
- Router .transitionTo (' user' , {id: 10 }, {showAge: true });
176
- Router .transitionTo (' about' );
177
- ```
178
-
179
- ** replaceWith(routeName, [ params[ , query]] )** - Programatically replace current route with a new route. Does not add an entry into the browser history.
180
-
181
- ``` js
182
- Router .replaceWith (' user' , {id: 10 }, {showAge: true });
183
- Router .replaceWith (' about' );
184
- ```
185
-
186
- ### Route (Component)
140
+ ### Route (component)
187
141
188
142
Configuration component to declare your application's routes and view hierarchy.
189
143
190
- #### Properties
144
+ #### Props
145
+
146
+ ** location** - The method to use for page navigation when initializing the router.
147
+ May be either "hash" to use URLs with hashes in them and the ` hashchange ` event or
148
+ "history" to use the HTML5 history API. This prop is only ever used on the root
149
+ route that is rendered into the page.
191
150
192
151
** name** - The name of the route, used in the ` Link ` component and the
193
152
router's transition methods.
194
153
195
154
** path** - The path used in the URL, supporting dynamic segments. If
196
- left undefined, the path will be defined by the ` name ` .
155
+ left undefined, the path will be defined by the ` name ` . This path is always
156
+ absolute from the URL "root", even if the leading slash is left off. Nested
157
+ routes do not inherit the path of their parent.
197
158
198
159
** handler** - The component to be rendered when the route matches.
199
160
@@ -208,10 +169,10 @@ handler will have an instance of the child route's handler available on
208
169
``` xml
209
170
<Route handler ={App}>
210
171
<!-- path is automatically assigned to the name since it is omitted -->
211
- <Route name =" about" handler ={About} />
172
+ <Route name =" about" handler ={About}/>
212
173
<Route name =" users" handler ={Users}>
213
174
<!-- note the dynamic segment in the path -->
214
- <Route name =" user" handler ={User} path =" /user/:id" />
175
+ <Route name =" user" handler ={User} path =" /user/:id" />
215
176
</Route >
216
177
</Route >
217
178
```
@@ -227,44 +188,13 @@ Route({handler: App},
227
188
);
228
189
```
229
190
230
- #### Note
191
+ ### Route Handler (user-defined component)
231
192
232
- Paths are not inherited from parent routes.
233
-
234
- ### Link (Component)
193
+ The value you pass to a route's ` handler ` prop is another component that
194
+ is rendered to the page when that route is active. There are some special
195
+ props and static methods available to these components.
235
196
236
- Creates an anchor tag that links to a route in the application. Also
237
- gets the ` active ` class automatically when the route matches. If you
238
- change the path of your route, you don't have to change your links.
239
-
240
- #### Properties
241
-
242
- ** to** - The name of the route to link to.
243
-
244
- ** query** - Object, Query parameters to add to the link. Access query
245
- parameters in your route handler with ` this.props.query ` .
246
-
247
- ** [ param] ** - Any parameters the route defines are passed by name
248
- through the link's properties.
249
-
250
- #### Example
251
-
252
- Given a route like ` <Route name="user" path="/users/:userId"/> ` :
253
-
254
- ``` xml
255
- <Link to =" user" userId ={user.id} params ={{foo: bar}}>{user.name}</Link >
256
- <!-- becomes one of these depending on your router and if the route is
257
- active -->
258
- <a href =" /users/123?foo=bar" class =" active" >Michael</a >
259
- <a href =" #/users/123?foo=bar" >Michael</a >
260
- ```
261
-
262
- ### Handlers
263
-
264
- There are some properties and hooks available to the handlers you pass
265
- to the ` handler ` property of a ` Route ` .
266
-
267
- #### Properties
197
+ #### Props
268
198
269
199
** this.props.activeRoute** - The active child route handler instance.
270
200
Use it in your render method to render the child route.
@@ -275,7 +205,7 @@ path="users/:userId"/>` the dynamic values are available at
275
205
276
206
** this.props.query** - The query parameters from the url.
277
207
278
- ### Transition Hooks
208
+ #### Static Methods ( Transition Hooks)
279
209
280
210
You can define static methods on your route handlers that will be called
281
211
during route transitions.
@@ -293,10 +223,12 @@ transition. The `component` is the current component, you'll probably
293
223
need it to check its state to decide if you want to allow the
294
224
transition.
295
225
296
- #### transition (object)
226
+ ### Transition (object)
297
227
298
228
** transition.abort()** - aborts a transition
299
229
230
+ ** transition.redirect(to, params, query)** - redirect to another route
231
+
300
232
** transition.retry()** - retrys a transition
301
233
302
234
#### Example
@@ -327,6 +259,59 @@ var Settings = React.createClass({
327
259
});
328
260
```
329
261
262
+ ### Link (Component)
263
+
264
+ Creates an anchor tag that links to a route in the application. Also
265
+ gets the ` active ` class automatically when the route matches. If you
266
+ change the path of your route, you don't have to change your links.
267
+
268
+ #### Properties
269
+
270
+ ** to** - The name of the route to link to.
271
+
272
+ ** query** - Object, Query parameters to add to the link. Access query
273
+ parameters in your route handler with ` this.props.query ` .
274
+
275
+ ** [ param] ** - Any parameters the route defines are passed by name
276
+ through the link's properties.
277
+
278
+ #### Example
279
+
280
+ Given a route like ` <Route name="user" path="/users/:userId"/> ` :
281
+
282
+ ``` xml
283
+ <Link to =" user" userId ={user.id} params ={{foo: bar}}>{user.name}</Link >
284
+ <!-- becomes one of these depending on your router and if the route is
285
+ active -->
286
+ <a href =" /users/123?foo=bar" class =" active" >Michael</a >
287
+ <a href =" #/users/123?foo=bar" >Michael</a >
288
+ ```
289
+
290
+
291
+ ### Top-Level Static Methods
292
+
293
+ The Router module has several top-level methods that may be used to navigate around the application.
294
+
295
+ ** transitionTo(routeName, [ params[ , query]] )** - Programatically transition to a new route.
296
+
297
+ ``` js
298
+ Router .transitionTo (' user' , {id: 10 }, {showAge: true });
299
+ Router .transitionTo (' about' );
300
+ ```
301
+
302
+ ** replaceWith(routeName, [ params[ , query]] )** - Programatically replace current route with a new route. Does not add an entry into the browser history.
303
+
304
+ ``` js
305
+ Router .replaceWith (' user' , {id: 10 }, {showAge: true });
306
+ Router .replaceWith (' about' );
307
+ ```
308
+
309
+ ** goBack()** - Programatically go back to the last route and remove the most recent entry from the browser history.
310
+
311
+ ``` js
312
+ Router .goBack ();
313
+ ```
314
+
330
315
Development
331
316
-----------
332
317
0 commit comments