Skip to content

Commit 9e7e04f

Browse files
committed
Merge pull request #40 from rpflorence/route-component
Route component
2 parents cd74bbd + e3fdc83 commit 9e7e04f

35 files changed

+1096
-1136
lines changed

README.md

+88-103
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ Features
99
--------
1010

1111
- Nested views mapped to nested routes
12+
- Modular construction of route hierarchy
13+
- Fully asynchronous transition hooks
14+
- Transition abort / redirect / retry
1215
- Dynamic segments
1316
- Query parameters
1417
- Links with automatic `.active` class when their route is active
15-
- Transition abort / retry
1618
- Multiple root routes
17-
- hash or history urls
19+
- Hash or HTML5 history URLs
1820

1921
Check out the `examples` directory to see how simple previously complex UI
2022
and workflows are to create.
@@ -27,38 +29,33 @@ Installation
2729
Usage
2830
-----
2931

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
3133
webpack or something that can load/bundle it.
3234

3335
```
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;
3837
39-
// note that you always need a root route like `App` here
40-
41-
Router(
38+
React.renderComponent((
4239
<Route handler={App}>
4340
<Route name="about" handler={About}/>
4441
<Route name="users" handler={Users}>
45-
<Route name="user" path="user/:userId" handler={User}/>
42+
<Route name="user" path="/user/:userId" handler={User}/>
4643
</Route>
4744
</Route>
48-
).renderComponent(document.body);
45+
), document.body);
4946
```
5047

5148
Or if JSX isn't your jam:
5249

5350
```js
54-
Router(
51+
React.renderComponent((
5552
Route({handler: App},
5653
Route({name: "about", handler: About}),
5754
Route({name: "users", handler: Users},
58-
Route({name="user", path: "user/:userId", handler: User})
55+
Route({name: "user", path: "/user/:userId", handler: User})
5956
)
6057
)
61-
).renderComponent(document.body);
58+
), document.body);
6259
```
6360

6461
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.
7168
Here's the rest of the application:
7269

7370
```js
71+
var Link = require('react-nested-router').Link;
72+
7473
var App = React.createClass({
7574
render: function() {
7675
return (
@@ -138,62 +137,24 @@ Benefits of This Approach
138137
API
139138
---
140139

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)
187141

188142
Configuration component to declare your application's routes and view hierarchy.
189143

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.
191150

192151
**name** - The name of the route, used in the `Link` component and the
193152
router's transition methods.
194153

195154
**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.
197158

198159
**handler** - The component to be rendered when the route matches.
199160

@@ -208,10 +169,10 @@ handler will have an instance of the child route's handler available on
208169
```xml
209170
<Route handler={App}>
210171
<!-- path is automatically assigned to the name since it is omitted -->
211-
<Route name="about" handler={About} />
172+
<Route name="about" handler={About}/>
212173
<Route name="users" handler={Users}>
213174
<!-- 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"/>
215176
</Route>
216177
</Route>
217178
```
@@ -227,44 +188,13 @@ Route({handler: App},
227188
);
228189
```
229190

230-
#### Note
191+
### Route Handler (user-defined component)
231192

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.
235196

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
268198

269199
**this.props.activeRoute** - The active child route handler instance.
270200
Use it in your render method to render the child route.
@@ -275,7 +205,7 @@ path="users/:userId"/>` the dynamic values are available at
275205

276206
**this.props.query** - The query parameters from the url.
277207

278-
### Transition Hooks
208+
#### Static Methods (Transition Hooks)
279209

280210
You can define static methods on your route handlers that will be called
281211
during route transitions.
@@ -293,10 +223,12 @@ transition. The `component` is the current component, you'll probably
293223
need it to check its state to decide if you want to allow the
294224
transition.
295225

296-
#### transition (object)
226+
### Transition (object)
297227

298228
**transition.abort()** - aborts a transition
299229

230+
**transition.redirect(to, params, query)** - redirect to another route
231+
300232
**transition.retry()** - retrys a transition
301233

302234
#### Example
@@ -327,6 +259,59 @@ var Settings = React.createClass({
327259
});
328260
```
329261

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+
330315
Development
331316
-----------
332317

examples/auth-flow/app.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
/** @jsx React.DOM */
22
var React = require('react');
3-
var ReactRouter = require('../../modules/main');
4-
var Router = ReactRouter.Router;
5-
var Route = ReactRouter.Route;
6-
var Link = ReactRouter.Link;
3+
var Router = require('../../modules/main');
4+
var Route = Router.Route;
5+
var Link = Router.Link;
76

87
var App = React.createClass({
98
getInitialState: function() {
@@ -179,12 +178,13 @@ function pretendRequest(email, pass, cb) {
179178
}
180179

181180

182-
Router(
181+
var routes = (
183182
<Route handler={App}>
184183
<Route name="login" handler={Login}/>
185184
<Route name="logout" handler={Logout}/>
186185
<Route name="about" handler={About}/>
187186
<Route name="dashboard" handler={Dashboard}/>
188187
</Route>
189-
).renderComponent(document.body);
188+
);
190189

190+
React.renderComponent(routes, document.body);

examples/data-flow/app.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
/** @jsx React.DOM */
22
var React = require('react');
3-
var ReactRouter = require('../../modules/main');
4-
var Router = ReactRouter.Router;
5-
var Route = ReactRouter.Route;
6-
var Link = ReactRouter.Link;
3+
var Router = require('../../modules/main');
4+
var Route = Router.Route;
5+
var Link = Router.Link;
76

87
// Taco store!
98

@@ -74,9 +73,10 @@ var Taco = React.createClass({
7473
}
7574
});
7675

77-
Router(
76+
var routes = (
7877
<Route handler={App}>
7978
<Route name="taco" path="taco/:name" handler={Taco}/>
8079
</Route>
81-
).renderComponent(document.body);
80+
);
8281

82+
React.renderComponent(routes, document.body);

examples/dynamic-segments/app.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
/** @jsx React.DOM */
22
var React = require('react');
3-
var ReactRouter = require('../../modules/main');
4-
var Router = ReactRouter.Router;
5-
var Route = ReactRouter.Route;
6-
var Link = ReactRouter.Link;
3+
var Router = require('../../modules/main');
4+
var Route = Router.Route;
5+
var Link = Router.Link;
76

87
var App = React.createClass({
98
render: function() {
@@ -45,11 +44,12 @@ var Task = React.createClass({
4544
}
4645
});
4746

48-
Router(
47+
var routes = (
4948
<Route handler={App}>
50-
<Route name="user" path="user/:userId" handler={User}>
51-
<Route name="task" path="user/:userId/tasks/:taskId" handler={Task}/>
49+
<Route name="user" path="/user/:userId" handler={User}>
50+
<Route name="task" path="/user/:userId/tasks/:taskId" handler={Task}/>
5251
</Route>
5352
</Route>
54-
).renderComponent(document.body);
53+
);
5554

55+
React.renderComponent(routes, document.body);

0 commit comments

Comments
 (0)