Skip to content

Commit

Permalink
Added deprecation message when adding an extra anchor in behalf of th…
Browse files Browse the repository at this point in the history
…e user. (#797)
  • Loading branch information
impronunciable authored and timneutkens committed Jan 16, 2017
1 parent 7cc554c commit 70b92e6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/link.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { resolve } from 'url'
import React, { Component, Children, PropTypes } from 'react'
import Router from './router'
import { warn, execOnce } from './utils'

export default class Link extends Component {
constructor (props) {
Expand Down Expand Up @@ -69,6 +70,7 @@ export default class Link extends Component {
if (isAnchor) {
return React.cloneElement(child, props)
} else {
warnLink(`Warning: Every Link must be the parent of an anchor, this pattern is deprecated. Please add an anchor inside the <Link>.`)
return <a {...props}>{child}</a>
}
})
Expand All @@ -82,3 +84,5 @@ export function isLocal (href) {
return !/^(https?:)?\/\//.test(href) ||
origin === href.substr(0, origin.length)
}

const warnLink = execOnce(warn)
10 changes: 10 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ export function warn (message) {
}
}

export function execOnce (fn) {
let used = false
return (...args) => {
if (!used) {
used = true
fn.apply(this, args)
}
}
}

export function deprecated (fn, message) {
if (process.env.NODE_ENV === 'production') return fn

Expand Down

0 comments on commit 70b92e6

Please sign in to comment.