File tree 6 files changed +77
-0
lines changed
examples/dynamic-segments
6 files changed +77
-0
lines changed Original file line number Diff line number Diff line change
1
+ module . exports = require ( './modules/components/Redirect' ) ;
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ React Router API
5
5
6
6
- Components
7
7
- [ ` Link ` ] ( /docs/api/components/Link.md )
8
+ - [ ` Redirect ` ] ( /docs/api/components/Redirect.md )
8
9
- [ ` Route ` ] ( /docs/api/components/Route.md )
9
10
- [ ` RouteHandler ` ] ( /docs/api/components/RouteHandler.md )
10
11
- [ ` Routes ` ] ( /docs/api/components/Routes.md )
Original file line number Diff line number Diff line change
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
+ ```
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ var React = require('react');
3
3
var Router = require ( '../../index' ) ;
4
4
var Route = Router . Route ;
5
5
var Routes = Router . Routes ;
6
+ var Redirect = Router . Redirect ;
6
7
var Link = Router . Link ;
7
8
8
9
var App = React . createClass ( {
@@ -50,6 +51,7 @@ var routes = (
50
51
< Route handler = { App } >
51
52
< Route name = "user" path = "/user/:userId" handler = { User } >
52
53
< Route name = "task" path = "/user/:userId/tasks/:taskId" handler = { Task } />
54
+ < Redirect from = "/user/:userId/todos/:taskId" to = "task" />
53
55
</ Route >
54
56
</ Route >
55
57
</ Routes >
Original file line number Diff line number Diff line change 1
1
exports . ActiveState = require ( './ActiveState' ) ;
2
2
exports . AsyncState = require ( './AsyncState' ) ;
3
3
exports . Link = require ( './Link' ) ;
4
+ exports . Redirect = require ( './Redirect' ) ;
4
5
exports . Route = require ( './Route' ) ;
5
6
exports . Routes = require ( './Routes' ) ;
6
7
exports . goBack = require ( './goBack' ) ;
Original file line number Diff line number Diff line change
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 ;
You can’t perform that action at this time.
0 commit comments