-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Add warning to link.js #3572
Conversation
Would it be better to place this warning in the render method? |
@@ -98,7 +98,9 @@ const Link = React.createClass({ | |||
if (allowTransition) { | |||
const { to, query, hash, state } = this.props | |||
const location = createLocationDescriptor(to, { query, hash, state }) | |||
|
|||
|
|||
warning(this.context.router != undefined, 'No router context, are you sure your link is inside a valid Router component?') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this to the top of onClick
and make it an invariant, something like
invariant(
this.context.router,
'<Link>s rendered outside of a router context cannot handle clicks.'
)
Not in |
@@ -98,7 +104,7 @@ const Link = React.createClass({ | |||
if (allowTransition) { | |||
const { to, query, hash, state } = this.props | |||
const location = createLocationDescriptor(to, { query, hash, state }) | |||
|
|||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove trailing whitespace here
LGTM aside from that whitespace nit. |
Done. |
Thanks. |
@@ -73,6 +74,11 @@ const Link = React.createClass({ | |||
}, | |||
|
|||
handleClick(event) { | |||
invariant( |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
What do we think here? I think putting the |
Well, given the problems with putting it on a normal lifecycle method, I guess that's out. I suppose having it on the click handler is better than nothing, but it still feels like we're not going to catch as many problems as we could. |
A while ago I suggested we should make |
Actually, that would be sort of counterproductive, since it means there'd be bugs in production that wouldn't get caught by tests. I dunno – it's hard. |
Yeah, unfortunately this isn't Rails where RAILS_ENV is de facto standard. And worse still, NODE_ENV doesn't exist in the browser context, so headless browser testing would break all of that anyways. |
Released with #3571 as v2.5.1. |
Any suggestions for unit testing now? I was using sinon to spy on |
I could move the warning down to below where we call an external onClick. When you have your own onClick, the Link doesn't requires access to the router context. |
Only if you Can you just inject a fake router context? And can you open an issue for this? |
I foolishly moved my Link component outside of the Router. I managed to figure it out pretty quickly but the error wasn't terribly helpful. See #3225. Thoughts?