Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add warning to link.js #3572

Merged
merged 4 commits into from
Jun 24, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions modules/Link.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react'
import warning from './routerWarning'
import invariant from 'invariant'
import { routerShape } from './PropTypes'

const { bool, object, string, func, oneOfType } = React.PropTypes
Expand Down Expand Up @@ -73,6 +74,11 @@ const Link = React.createClass({
},

handleClick(event) {
invariant(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move this to a lifecycle hook (probably componentWillMount) so developers can be alerted to mis-use earlier. I could see a Link not getting clicked on in testing and then going to production broken.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't – the idea is that for unit tests, we let the link be rendered without a router, so that people can test non-navigation-related aspects of their page components that use <Link>s without having to set up a router context.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a code flag for unit tests?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's NODE_ENV, but it's uncommon for people to set it.

this.context.router,
'<Link>s rendered outside of a router context cannot handle clicks.'
)

let allowTransition = true

if (this.props.onClick)
Expand Down