Skip to content

Commit 51a3731

Browse files
committed
Add route matching docs
1 parent 4c0e6b0 commit 51a3731

File tree

4 files changed

+60
-3
lines changed

4 files changed

+60
-3
lines changed

docs/Glossary.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ A *route pattern* (or "path") is a string that describes a portion of a URL. Pat
132132

133133
- `:paramName` – matches a URL segment up to the next `/`, `?`, or `#`. The matched string is called a [param](#params)
134134
- `()` – Wraps a portion of the URL that is optional
135-
- `*` – Matches all characters (non-greedy) up to the next character in the pattern, or to the end of the URL if there is none, and creates a `splat` param
135+
- `*` – Matches all characters (non-greedy) up to the next character in the pattern, or to the end of the URL if there is none, and creates a `splat` [param](#params)
136136

137137
Route patterns are relative to the pattern of the parent route unless they begin with a `/`, in which case they begin matching at the beginning of the URL.
138138

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- [Introduction](Introduction.md)
44
- [Route Configuration](RouteConfiguration.md)
5+
- [Route Matching](RouteMatching.md)
56
- [Dynamic Routing](DynamicRouting.md)
67
- [Advanced Usage](AdvancedUsage.md)
78
- [Server Rendering](ServerRendering.md)

docs/RouteConfiguration.md

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Route Configuration
22

3-
Let's start with the simple app from [the introduction](Introduction.md).
3+
A [route configuration](Glossary.md#routeconfig) is basically a set of instructions that tell a router how to try and [match the URL](RouteMatching.md) and what code to run when it does. To illustrate some of the features available in your route config, let's expand on the simple app from [the introduction](Introduction.md).
44

55
```js
66
import React from 'react';
@@ -67,7 +67,7 @@ URL | Components
6767

6868
### Adding an Index
6969

70-
Imagine we'd like to render another component inside of `App` when the URL is `/`. Currently, `this.props.children` is `undefined` in this case. We can use an `<IndexRoute>` for that.
70+
Imagine we'd like to render another component inside of `App` when the URL is `/`. Currently, `this.props.children` is `undefined` in this case. We can use an `<IndexRoute>` to specify a "default" page.
7171

7272
```js
7373
import { IndexRoute } from 'react-router';
@@ -133,6 +133,8 @@ URL | Components
133133

134134
The ability to use absolute paths in deeply nested routes gives us complete control over what the URL looks like! We don't have to add more segments to the URL just to get nested UI.
135135

136+
**Note**: Absolute paths may not be used in route config that is [dynamically loaded](DynamicLoading.md).
137+
136138
### Preserving URLs
137139

138140
But wait just a minute ... we just changed a URL! [That's not cool](http://www.w3.org/Provider/Style/URI.html). Now everyone who had a link to `/inbox/messages/5` has a **broken link**. :(
@@ -159,3 +161,34 @@ React.render((
159161
```
160162

161163
Now when someone clicks on that link to `/inbox/messages/5` they'll automatically be redirected to `/messages/5`. We don't hate our users anymore! :highfive:
164+
165+
### Alternate Configuration
166+
167+
Since [route](Glossary.md#route)s are usually nested, it's useful to use a concise nested syntax like [JSX](https://facebook.github.io/jsx/) to describe their relationship to one another. However, you may use an array of [route](Glossary.md#route) objects if you prefer to avoid using JSX.
168+
169+
The route config we've discussed up to this point could also be specified like this:
170+
171+
```js
172+
var routeConfig = [
173+
{ path: '/',
174+
component: App,
175+
indexRoute: { component: Dashboard },
176+
childRoutes: [
177+
{ path: 'about', component: About },
178+
{ path: 'inbox',
179+
component: Inbox,
180+
childRoutes: [
181+
{ path: '/messages/:id', component: Message },
182+
{ path: 'messages/:id',
183+
onEnter: function (nextState, redirectTo) {
184+
redirectTo('/messages/' + nextState.params.id);
185+
}
186+
}
187+
]
188+
}
189+
]
190+
}
191+
];
192+
193+
React.render(<Router routes={routeConfig} />, document.body);
194+
```

docs/RouteMatching.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## Route Matching
2+
3+
A [route](Glossary.md#route) has two attributes that determine whether or not it "matches" the URL: 1) [nesting](#nesting) and 2) its [`path`](#pathsyntax).
4+
5+
### Nesting
6+
7+
React Router uses the concept of nested routes to let you declare nested sets of views that should be rendered when a given URL is invoked. Nested routes are arranged in a tree-like structure. To find a match, React Router traverses the [route config](Glossary.md#routeconfig) depth-first searching for a route that matches the URL.
8+
9+
### Path Syntax
10+
11+
A route path is [a string pattern](Glossary.md#routepattern) that is used to match a URL (or a portion of one). Route paths are interpreted literally, except for the following special symbols:
12+
13+
- `:paramName` – matches a URL segment up to the next `/`, `?`, or `#`. The matched string is called a [param](Glossary.md#params)
14+
- `()` – Wraps a portion of the URL that is optional
15+
- `*` – Matches all characters (non-greedy) up to the next character in the pattern, or to the end of the URL if there is none, and creates a `splat` [param](Glossary.md#params)
16+
17+
```
18+
<Route path="/hello/:name"> // matches /hello/michael and /hello/ryan
19+
<Route path="/hello(/:name)"> // matches /hello, /hello/michael, and /hello/ryan
20+
<Route path="/files/*.*"> // matches /files/hello.jpg and /files/path/to/hello.jpg
21+
```
22+
23+
If a route uses a relative `path`, it builds upon the accumulated `path` of its ancestors. Nested routes may opt-out of this behavior by [using an absolute `path`](RouteConfiguration.md#decoupling-the-ui-from-the-url).

0 commit comments

Comments
 (0)