diff --git a/lib/link.js b/lib/link.js index 4132f1b051f32..e352eec5df76d 100644 --- a/lib/link.js +++ b/lib/link.js @@ -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) { @@ -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 .`) return {child} } }) @@ -82,3 +84,5 @@ export function isLocal (href) { return !/^(https?:)?\/\//.test(href) || origin === href.substr(0, origin.length) } + +const warnLink = execOnce(warn) diff --git a/lib/utils.js b/lib/utils.js index f9116cb952103..7ffff02981e9e 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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