1
- import { createClass , createElement , PropTypes } from 'react' ;
1
+ import React from 'react' ;
2
+ import warning from 'warning' ;
2
3
3
- var { object, string, func } = PropTypes ;
4
+ var { object, string, func } = React . PropTypes ;
4
5
5
6
function isLeftClickEvent ( event ) {
6
7
return event . button === 0 ;
@@ -11,24 +12,24 @@ function isModifiedEvent(event) {
11
12
}
12
13
13
14
/**
14
- * <Link> components are used to create an <a> element that links to a route.
15
- * When that route is active, the link gets an "active" class name (or the
16
- * value of its `activeClassName` prop).
15
+ * A <Link> is used to create an <a> element that links to a route.
16
+ * When that route is active, the link gets an "active" class name
17
+ * (or the value of its `activeClassName` prop).
17
18
*
18
19
* For example, assuming you have the following route:
19
20
*
20
- * <Route name="showPost" path="/posts/:postID" handler ={Post}/>
21
+ * <Route path="/posts/:postID" component ={Post} />
21
22
*
22
23
* You could use the following component to link to that route:
23
24
*
24
25
* <Link to={`/posts/${post.id}`} />
25
26
*
26
- * Links may pass along query string parameters
27
- * using the ` query` prop .
27
+ * Links may pass along location state and/or query string parameters
28
+ * in the state/ query props, respectively .
28
29
*
29
- * <Link to="/posts/123" query={{ show: true }}/>
30
+ * <Link ... query={{ show: true }} state={{ the: 'state' }} />
30
31
*/
31
- var Link = createClass ( {
32
+ var Link = React . createClass ( {
32
33
33
34
contextTypes : {
34
35
router : object
@@ -67,33 +68,43 @@ var Link = createClass({
67
68
event . preventDefault ( ) ;
68
69
69
70
if ( allowTransition )
70
- this . context . router . transitionTo ( this . props . to , this . props . query , this . props . state ) ;
71
+ this . context . router . pushState ( this . props . state , this . props . to , this . props . query ) ;
72
+ } ,
73
+
74
+ componentWillMount ( ) {
75
+ warning (
76
+ this . context . router ,
77
+ 'A <Link> should not be rendered outside the context of a router; ' +
78
+ 'some features including real hrefs, active styling, and navigation ' +
79
+ 'will not function correctly'
80
+ ) ;
71
81
} ,
72
82
73
83
render ( ) {
74
- var { router } = this . context ;
84
+ var { to , query } = this . props ;
75
85
76
- var props = Object . assign ( { } , this . props , {
86
+ var props = {
87
+ ...this . props ,
77
88
onClick : this . handleClick
78
- } ) ;
89
+ } ;
79
90
80
- // Ignore if rendered outside of the context of a
81
- // router, simplifies unit testing.
82
- if ( router ) {
83
- var { to, query } = this . props ;
91
+ var { router } = this . context ;
84
92
93
+ // Ignore if rendered outside the context
94
+ // of a router, simplifies unit testing.
95
+ if ( router ) {
85
96
props . href = router . createHref ( to , query ) ;
86
97
87
98
if ( router . isActive ( to , query ) ) {
88
99
if ( props . activeClassName )
89
100
props . className += props . className !== '' ? ` ${ props . activeClassName } ` : props . activeClassName ;
90
101
91
102
if ( props . activeStyle )
92
- props . style = Object . assign ( { } , props . style , props . activeStyle ) ;
103
+ props . style = { ... props . style , ... props . activeStyle } ;
93
104
}
94
105
}
95
106
96
- return createElement ( 'a' , props ) ;
107
+ return React . createElement ( 'a' , props ) ;
97
108
}
98
109
99
110
} ) ;
0 commit comments