11import React from "react" ;
22import { __RouterContext as RouterContext } from "react-router" ;
3- import { createLocation } from "history" ;
43import PropTypes from "prop-types" ;
54import invariant from "tiny-invariant" ;
5+ import { resolveToLocation , normalizeToLocation } from "./utils/locationUtils" ;
66
77function isModifiedEvent ( event ) {
88 return ! ! ( event . metaKey || event . altKey || event . ctrlKey || event . shiftKey ) ;
@@ -12,7 +12,7 @@ function isModifiedEvent(event) {
1212 * The public API for rendering a history-aware <a>.
1313 */
1414class Link extends React . Component {
15- handleClick ( event , history ) {
15+ handleClick ( event , context ) {
1616 try {
1717 if ( this . props . onClick ) this . props . onClick ( event ) ;
1818 } catch ( ex ) {
@@ -28,9 +28,13 @@ class Link extends React.Component {
2828 ) {
2929 event . preventDefault ( ) ;
3030
31- const method = this . props . replace ? history . replace : history . push ;
31+ const location = resolveToLocation ( this . props . to , context . location ) ;
3232
33- method ( this . props . to ) ;
33+ const method = this . props . replace
34+ ? context . history . replace
35+ : context . history . push ;
36+
37+ method ( location ) ;
3438 }
3539 }
3640
@@ -42,16 +46,17 @@ class Link extends React.Component {
4246 { context => {
4347 invariant ( context , "You should not use <Link> outside a <Router>" ) ;
4448
45- const location =
46- typeof to === "string"
47- ? createLocation ( to , null , null , context . location )
48- : to ;
49+ const location = normalizeToLocation (
50+ resolveToLocation ( to , context . location ) ,
51+ context . location
52+ ) ;
53+
4954 const href = location ? context . history . createHref ( location ) : "" ;
5055
5156 return (
5257 < a
5358 { ...rest }
54- onClick = { event => this . handleClick ( event , context . history ) }
59+ onClick = { event => this . handleClick ( event , context ) }
5560 href = { href }
5661 ref = { innerRef }
5762 />
@@ -63,7 +68,11 @@ class Link extends React.Component {
6368}
6469
6570if ( __DEV__ ) {
66- const toType = PropTypes . oneOfType ( [ PropTypes . string , PropTypes . object ] ) ;
71+ const toType = PropTypes . oneOfType ( [
72+ PropTypes . string ,
73+ PropTypes . object ,
74+ PropTypes . func
75+ ] ) ;
6776 const innerRefType = PropTypes . oneOfType ( [
6877 PropTypes . string ,
6978 PropTypes . func ,
0 commit comments