Skip to content

Commit 59dfba9

Browse files
committed
route docs
1 parent 03ae71e commit 59dfba9

File tree

2 files changed

+49
-47
lines changed

2 files changed

+49
-47
lines changed
Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
1+
## Route
2+
13
A `Route` is used to declaratively map routes to your application's
24
component hierarchy.
35

4-
Props
5-
-----
6+
### Props
67

7-
### `path`
8+
#### `path`
89

910
The path used in the URL.
1011

11-
It will concat with the parent route's path unless it starts with `/`.
12-
In which case you will need to use `absoluteChildPaths` on the parent
13-
route so the router knows to keep going down the route tree even though
14-
the parent path doesn't match the url.
12+
It will concat with the parent route's path unless it starts with `/`,
13+
making it an absolute path.
1514

16-
If left undefined, the router will try to match the child routes.
15+
**Note**: Absolute paths may not be used in route config that is [dynamically loaded](DynamicRouting.md).
1716

18-
Please refer to the [Path Matching Guide][path-matching] to learn more
19-
about supported path matching syntax.
17+
If left undefined, the router will try to match the child routes.
2018

21-
### `component`
19+
#### `component`
2220

2321
A single component to be rendered when the route matches the url. It can
2422
be rendered by the parent route component with `this.props.children`.
2523

26-
#### Example
27-
2824
```js
2925
var routes = (
3026
<Route component={App}>
@@ -45,19 +41,18 @@ var App = React.createClass({
4541
});
4642
```
4743

48-
### `components`
44+
#### `components`
4945

50-
Routes can define multiple components as an object of name:component
46+
Routes can define multiple components as an object of `name:component`
5147
pairs to be rendered when the path matches the url. They can be rendered
52-
by the parent route component with `this.props[name]`.
48+
by the parent route component with `this.props.children[name]`.
5349

54-
#### Example
50+
##### Example
5551

5652
```js
5753
// think of it outside the context of the router, if you had pluggable
5854
// portions of your `render`, you might do it like this
59-
<App main={<Users/>} sidebar={<UsersSidebar/>}/>
60-
<App main={<Groups/>} sidebar={<GroupsSidebar/>}/>
55+
<App children={{main: <Users/>, sidebar: <UsersSidebar/>}}/>
6156

6257
// So with the router it looks like this:
6358
var routes = (
@@ -71,14 +66,14 @@ var routes = (
7166

7267
var App = React.createClass({
7368
render () {
74-
// the matched child route components become props in the parent
69+
var { main, sidebar } = this.props.children;
7570
return (
7671
<div>
7772
<div className="Main">
78-
{this.props.main}
73+
{main}
7974
</div>
8075
<div className="Sidebar">
81-
{this.props.sidebar}
76+
{sidebar}
8277
</div>
8378
</div>
8479
);
@@ -96,48 +91,53 @@ var Users = React.createClass({
9691
);
9792
}
9893
});
99-
10094
```
10195

102-
### `getComponents(state, cb)`
96+
#### `getComponent(callback)`
10397

104-
Same as `components` but asynchronous, useful for code-splitting and
105-
returning different routes given some transition `state`.
98+
Same as `component` but asynchronous, useful for
99+
code-splitting.
106100

107-
#### `callback` signature
101+
##### `callback` signature
108102

109103
`cb(err, component)`
110104

111-
#### Example
105+
##### Example
112106

113107
```js
114-
<Route path="coures/:courseId" getComponents={(cb) => {
108+
<Route path="coures/:courseId" getComponent={(cb) => {
115109
// do asynchronous stuff to find the components
116-
cb(null, [Course]);
110+
cb(null, Course);
117111
}}/>
118112
```
119113

120-
### `children`
114+
#### `getComponents(callback)`
115+
116+
Same as `components` but asynchronous, useful for
117+
code-splitting.
118+
119+
##### `callback` signature
120+
121+
`cb(err, components)`
122+
123+
##### Example
124+
125+
```js
126+
<Route path="coures/:courseId" getComponent={(cb) => {
127+
// do asynchronous stuff to find the components
128+
cb(null, {sidebar: CourseSidebar, content: Course});
129+
}}/>
130+
```
121131

122-
Routes can be nested, `this.props.children` will contain the element
123-
created from the child route component. Please refer to the
124-
[overview][overview] since this is a very critical part of the router's
125-
design.
132+
#### `children`
126133

127-
### `onEnter(nextState, transition)`
134+
Routes can be nested, `this.props.children` will contain the element created from the child route component. Please refer to the [Route Configuration][RouteConfiguration.md] since this is a very critical part of the router's design.
128135

129-
Called when a route is about to be entered. It provides the next router
130-
state and the [transition][Transition] instance for cancelling/redirecting.
136+
#### `onEnter(nextState, redirectTo)`
131137

132-
### `onLeave(nextState, transition)`
138+
Called when a route is about to be entered. It provides the next router state and a function to redirect to another path.
133139

134-
Called when a route is about to be exited. It provides the next router
135-
state and the [transition][Transition] instance for cancelling/redirecting.
140+
#### `onLeave()`
136141

137-
[overview]:#TODO
138-
[path-matching]:#TODO
139-
[ignoreScrollBehavior]:#TODO
140-
[instragram-example]:#TODO
141-
[history]:#TODO
142-
[Transition]:#TODO
142+
Called when a route is about to be exited.
143143

modules/getComponents.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { mapAsync } from './AsyncUtils';
33
function getComponentsForRoute(route, callback) {
44
if (route.component || route.components) {
55
callback(null, route.component || route.components);
6+
} else if (route.getComponent) {
7+
route.getComponent(callback);
68
} else if (route.getComponents) {
79
route.getComponents(callback);
810
} else {

0 commit comments

Comments
 (0)