1- import { createClass , createElement , PropTypes } from 'react' ;
1+ import React from 'react' ;
2+ import warning from 'warning' ;
23
3- var { object, string, func } = PropTypes ;
4+ var { object, string, func } = React . PropTypes ;
45
56function isLeftClickEvent ( event ) {
67 return event . button === 0 ;
@@ -11,24 +12,24 @@ function isModifiedEvent(event) {
1112}
1213
1314/**
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).
1718 *
1819 * For example, assuming you have the following route:
1920 *
20- * <Route name="showPost" path="/posts/:postID" handler ={Post}/>
21+ * <Route path="/posts/:postID" component ={Post} />
2122 *
2223 * You could use the following component to link to that route:
2324 *
2425 * <Link to={`/posts/${post.id}`} />
2526 *
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 .
2829 *
29- * <Link to="/posts/123" query={{ show: true }}/>
30+ * <Link ... query={{ show: true }} state={{ the: 'state' }} />
3031 */
31- var Link = createClass ( {
32+ var Link = React . createClass ( {
3233
3334 contextTypes : {
3435 router : object
@@ -67,33 +68,43 @@ var Link = createClass({
6768 event . preventDefault ( ) ;
6869
6970 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+ ) ;
7181 } ,
7282
7383 render ( ) {
74- var { router } = this . context ;
84+ var { to , query } = this . props ;
7585
76- var props = Object . assign ( { } , this . props , {
86+ var props = {
87+ ...this . props ,
7788 onClick : this . handleClick
78- } ) ;
89+ } ;
7990
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 ;
8492
93+ // Ignore if rendered outside the context
94+ // of a router, simplifies unit testing.
95+ if ( router ) {
8596 props . href = router . createHref ( to , query ) ;
8697
8798 if ( router . isActive ( to , query ) ) {
8899 if ( props . activeClassName )
89100 props . className += props . className !== '' ? ` ${ props . activeClassName } ` : props . activeClassName ;
90101
91102 if ( props . activeStyle )
92- props . style = Object . assign ( { } , props . style , props . activeStyle ) ;
103+ props . style = { ... props . style , ... props . activeStyle } ;
93104 }
94105 }
95106
96- return createElement ( 'a' , props ) ;
107+ return React . createElement ( 'a' , props ) ;
97108 }
98109
99110} ) ;
0 commit comments