Skip to content

Commit b82f695

Browse files
committed
Merge pull request #3155 from taion/improve-redirect-docs
Improve redirect docs
2 parents 0768baa + bc93859 commit b82f695

File tree

3 files changed

+43
-28
lines changed

3 files changed

+43
-28
lines changed

docs/API.md

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -460,18 +460,7 @@ All the same props as [Route](#route) except for `path`.
460460
### `<IndexRedirect>`
461461
An `<IndexRedirect>` allows you to redirect from the URL of a parent route to another route. They can be used to allow a child route to serve as the default route for its parent, while still keeping a distinct URL.
462462

463-
#### Example
464-
```js
465-
<Router>
466-
<Route path="/" component={App}>
467-
<IndexRedirect to="groups" />
468-
{/* If no child route is matched, will redirect to 'groups' */}
469-
470-
<Route path="groups" component={Groups} />
471-
<Route path="users" component={Users} />
472-
</Route>
473-
</Router>
474-
```
463+
Please see the [Index Routes guide](/docs/guides/IndexRoutes.md).
475464

476465
#### Props
477466
All the same props as [Redirect](#redirect) except for `from`.

docs/guides/IndexRoutes.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,43 @@ the router allows you to have `Home` be a first class route component with
3737
Now `App` can render `{this.props.children}` and we have a first-class
3838
route for `Home` that can participate in routing.
3939

40+
## Index Redirects
41+
42+
Suppose your basic route configuration looks like:
43+
44+
```js
45+
<Route path="/" component={App}>
46+
<Route path="welcome" component={Welcome} />
47+
<Route path="about" component={About} />
48+
</Route>
49+
```
50+
51+
Suppose you want to redirect `/` to `/welcome`. To do this, you need to set up
52+
an index route that does the redirect. To do this, use the `<IndexRedirect>`
53+
component:
54+
55+
```js
56+
<Route path="/" component={App}>
57+
<IndexRedirect to="/welcome" />
58+
<Route path="welcome" component={Welcome} />
59+
<Route path="about" component={About} />
60+
</Route>
61+
```
62+
63+
This is equivalent to setting up an index route with just an `onEnter` hook
64+
that redirects the user. You would set this up with plain routes as:
65+
66+
```js
67+
const routes = [{
68+
path: '/', component: App,
69+
indexRoute: { onEnter: (nextState, replace) => replace('/welcome') },
70+
childRoutes: [
71+
{ path: 'welcome', component: Welcome },
72+
{ path: 'about', component: About }
73+
]
74+
}]
75+
```
76+
4077
## Index Links
4178

4279
If you were to `<Link to="/">Home</Link>` in this app, it would always

docs/guides/RouteConfiguration.md

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,16 @@ Continuing with our example above, if a user clicked on a link to `/about` from
175175
- `onLeave` on the `/inbox` route
176176
- `onEnter` on the `/about` route
177177

178-
### Alternate Configuration
178+
### Configuration with Plain Routes
179179

180180
Since [routes](/docs/Glossary.md#route) 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 also use an array of plain [route](/docs/Glossary.md#route) objects if you prefer to avoid using JSX.
181181

182+
The `<Redirect>` configuration helper is not available when using plain routes, so you have to set up the redirect using the `onEnter` hook.
183+
182184
The route config we've discussed up to this point could also be specified like this:
183185

184186
```js
185-
const routeConfig = [
187+
const routes = [
186188
{ path: '/',
187189
component: App,
188190
indexRoute: { component: Dashboard },
@@ -203,18 +205,5 @@ const routeConfig = [
203205
}
204206
]
205207

206-
render(<Router routes={routeConfig} />, document.body)
207-
```
208-
209-
### Redirect using plain routes configuration
210-
211-
```js
212-
const routes = [{
213-
path: '/',
214-
component: Home,
215-
onEnter: (nextState, replace) => replace('/about')
216-
}, {
217-
path: '/about',
218-
component: About
219-
}]
208+
render(<Router routes={routes} />, document.body)
220209
```

0 commit comments

Comments
 (0)