@@ -7,73 +7,69 @@ import { falsy } from './PropTypes'
7
7
const { string, object } = React . PropTypes
8
8
9
9
/**
10
- * A <Redirect> is used to declare another URL path a client should be sent
11
- * to when they request a given URL.
10
+ * A <Redirect> is used to declare another URL path a client should
11
+ * be sent to when they request a given URL.
12
12
*
13
- * Redirects are placed alongside routes in the route configuration and are
14
- * traversed in the same manner.
13
+ * Redirects are placed alongside routes in the route configuration
14
+ * and are traversed in the same manner.
15
15
*/
16
- const Redirect = React . createClass ( {
17
-
18
- statics : {
19
-
20
- createRouteFromReactElement ( element ) {
21
- const route = createRouteFromReactElement ( element )
22
-
23
- if ( route . from )
24
- route . path = route . from
25
-
26
- route . onEnter = function ( nextState , replaceState ) {
27
- const { location, params } = nextState
28
-
29
- let pathname
30
- if ( route . to . charAt ( 0 ) === '/' ) {
31
- pathname = formatPattern ( route . to , params )
32
- } else if ( ! route . to ) {
33
- pathname = location . pathname
34
- } else {
35
- let routeIndex = nextState . routes . indexOf ( route )
36
- let parentPattern = Redirect . getRoutePattern ( nextState . routes , routeIndex - 1 )
37
- let pattern = parentPattern . replace ( / \/ * $ / , '/' ) + route . to
38
- pathname = formatPattern ( pattern , params )
39
- }
40
-
41
- replaceState (
42
- route . state || location . state ,
43
- pathname ,
44
- route . query || location . query
45
- )
16
+ class Redirect extends React . Component {
17
+
18
+ static createRouteFromReactElement ( element ) {
19
+ const route = createRouteFromReactElement ( element )
20
+
21
+ if ( route . from )
22
+ route . path = route . from
23
+
24
+ route . onEnter = function ( nextState , replaceState ) {
25
+ const { location, params } = nextState
26
+
27
+ let pathname
28
+ if ( route . to . charAt ( 0 ) === '/' ) {
29
+ pathname = formatPattern ( route . to , params )
30
+ } else if ( ! route . to ) {
31
+ pathname = location . pathname
32
+ } else {
33
+ let routeIndex = nextState . routes . indexOf ( route )
34
+ let parentPattern = Redirect . getRoutePattern ( nextState . routes , routeIndex - 1 )
35
+ let pattern = parentPattern . replace ( / \/ * $ / , '/' ) + route . to
36
+ pathname = formatPattern ( pattern , params )
46
37
}
47
38
48
- return route
49
- } ,
39
+ replaceState (
40
+ route . state || location . state ,
41
+ pathname ,
42
+ route . query || location . query
43
+ )
44
+ }
50
45
51
- getRoutePattern ( routes , routeIndex ) {
52
- let parentPattern = ''
46
+ return route
47
+ }
53
48
54
- for ( let i = routeIndex ; i >= 0 ; i -- ) {
55
- let route = routes [ i ]
56
- let pattern = route . path || ''
57
- parentPattern = pattern . replace ( / \/ * $ / , '/' ) + parentPattern
49
+ static getRoutePattern ( routes , routeIndex ) {
50
+ let parentPattern = ''
58
51
59
- if ( pattern . indexOf ( '/' ) === 0 )
60
- break
61
- }
52
+ for ( let i = routeIndex ; i >= 0 ; i -- ) {
53
+ let route = routes [ i ]
54
+ let pattern = route . path || ''
55
+ parentPattern = pattern . replace ( / \/ * $ / , '/' ) + parentPattern
62
56
63
- return '/' + parentPattern
57
+ if ( pattern . indexOf ( '/' ) === 0 )
58
+ break
64
59
}
65
60
66
- } ,
61
+ return '/' + parentPattern
62
+ }
67
63
68
- propTypes : {
64
+ static propTypes = {
69
65
path : string ,
70
66
from : string , // Alias for path
71
67
to : string . isRequired ,
72
68
query : object ,
73
69
state : object ,
74
70
onEnter : falsy ,
75
71
children : falsy
76
- } ,
72
+ }
77
73
78
74
render ( ) {
79
75
invariant (
@@ -82,6 +78,6 @@ const Redirect = React.createClass({
82
78
)
83
79
}
84
80
85
- } )
81
+ }
86
82
87
83
export default Redirect
0 commit comments