Skip to content

Commit b959e3a

Browse files
committed
updated README
1 parent 9fffe49 commit b959e3a

File tree

2 files changed

+113
-14
lines changed

2 files changed

+113
-14
lines changed

Diff for: LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2014 Ryan Florence
1+
Copyright (c) 2014 Ryan Florence, Michael Jackson
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy of
44
this software and associated documentation files (the "Software"), to deal in

Diff for: README.md

+112-13
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,64 @@ React Router
33

44
Declarative routing with automatic UI nesting for React.
55

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+
------------
822

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

1227
WIP
1328
---
1429

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

17-
Usage Example
18-
-------------
32+
Usage
33+
-----
1934

2035
```js
36+
var Router = require('react-router');
37+
var Route = Router.Route;
38+
var Link = Router.Link;
39+
2140
Router(
2241
<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}>
2544
<Route name="user" path="user/:userId" handler={User}/>
2645
</Route>
2746
</Route>
2847
).renderComponent(document.body);
2948
```
3049

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:
3258

3359
```js
3460
var App = React.createClass({
3561
render: function() {
3662
return (
37-
<div className="App">
38-
<h1>App</h1>
63+
<div>
3964
<ul>
4065
<li><Link to="about">About</Link></li>
4166
<li><Link to="users">Users</Link></li>
@@ -56,7 +81,7 @@ var About = React.createClass({
5681
var Users = React.createClass({
5782
render: function() {
5883
return (
59-
<div className="Users">
84+
<div>
6085
<h2>Users</h2>
6186
{this.props.activeRoute}
6287
</div>
@@ -167,6 +192,73 @@ Route({handler: App},
167192

168193
Paths are not inherited from parent routes.
169194

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+
```
170262

171263
### Link (Component)
172264

@@ -225,3 +317,10 @@ Development
225317
`script/test` will fire up a karma runner, `npm test` will do the same
226318
but with the `--single-run` option.
227319

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

Comments
 (0)