Skip to content

Commit 829a9ec

Browse files
committed
[added] <Redirect/> component
closes #159
1 parent c906506 commit 829a9ec

File tree

6 files changed

+77
-0
lines changed

6 files changed

+77
-0
lines changed

Redirect.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./modules/components/Redirect');

docs/api/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ React Router API
55

66
- Components
77
- [`Link`](/docs/api/components/Link.md)
8+
- [`Redirect`](/docs/api/components/Redirect.md)
89
- [`Route`](/docs/api/components/Route.md)
910
- [`RouteHandler`](/docs/api/components/RouteHandler.md)
1011
- [`Routes`](/docs/api/components/Routes.md)

docs/api/components/Redirect.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
API: `Redirect` (component)
2+
===========================
3+
4+
Configures a redirect for a path in your route declarations.
5+
6+
Props
7+
-----
8+
9+
### `from`
10+
11+
The path you want to redirect from, including dynamic segments.
12+
13+
### `to`
14+
15+
The `name` of the route you want to redirect to.
16+
17+
Example
18+
-------
19+
20+
```xml
21+
<!--
22+
lets say we want to change from `/profile/123` to `/about/123`
23+
and redirect `/get-in-touch` to `/contact`
24+
-->
25+
<Routes>
26+
<Route handler={App}>
27+
<Route name="contact" handler={Contact}/>
28+
<Route name="about-user" path="about/:userId" handler={UserProfile}/>
29+
</Route>
30+
31+
<Redirect from="get-in-touch" to="contact" />
32+
<Redirect from="profile/:userId" to="about-user" />
33+
</Routes>
34+
```
35+
36+
Note that the `<Redirect/>` can be placed anywhere in the route
37+
hierarchy, if you'd prefer the redirects to be next to their respective
38+
routes.
39+
40+
```xml
41+
<Routes>
42+
<Route handler={App}>
43+
<Route name="contact" handler={Contact}/>
44+
<Redirect from="get-in-touch" to="contact" />
45+
</Route>
46+
</Routes>
47+
```

examples/dynamic-segments/app.js

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var React = require('react');
33
var Router = require('../../index');
44
var Route = Router.Route;
55
var Routes = Router.Routes;
6+
var Redirect = Router.Redirect;
67
var Link = Router.Link;
78

89
var App = React.createClass({
@@ -50,6 +51,7 @@ var routes = (
5051
<Route handler={App}>
5152
<Route name="user" path="/user/:userId" handler={User}>
5253
<Route name="task" path="/user/:userId/tasks/:taskId" handler={Task}/>
54+
<Redirect from="/user/:userId/todos/:taskId" to="task"/>
5355
</Route>
5456
</Route>
5557
</Routes>

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
exports.ActiveState = require('./ActiveState');
22
exports.AsyncState = require('./AsyncState');
33
exports.Link = require('./Link');
4+
exports.Redirect = require('./Redirect');
45
exports.Route = require('./Route');
56
exports.Routes = require('./Routes');
67
exports.goBack = require('./goBack');

modules/components/Redirect.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var React = require('react');
2+
var Route = require('./Route');
3+
4+
function Redirect(props) {
5+
return Route({
6+
path: props.from,
7+
handler: createRedirectClass(props.to)
8+
});
9+
}
10+
11+
function createRedirectClass(to) {
12+
return React.createClass({
13+
statics: {
14+
willTransitionTo: function(transition, params, query) {
15+
transition.redirect(to, params, query);
16+
}
17+
},
18+
19+
render: function() {
20+
return null;
21+
}
22+
});
23+
}
24+
25+
module.exports = Redirect;

0 commit comments

Comments
 (0)