-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Allow <Link> to be used outside of Router, add warning #3896
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
Conversation
I have a few questions:
|
let's just not handle clicks and not check isActive without a router in
|
(was intended but just an oversight)
|
@ryanflorence Do we want to warn users that it is not handling clicks? If so, where, onClick? Because when you click on a Link outside of a Router it still clicks and goes to the href. |
30cca4b
to
377fc86
Compare
Rebased to remove the warning. Now this commit refactors the method signature of getIsActive to allow for the sniffing of router context. Then getIsActive returns false if the router is not available on the context. It does this without any warning to the user. This also refactors away verbosity from the className assignment when returning the final anchor tag. As of right now clicking on this link outside of the router just follows its href. It seems like there is no problem occurring with handling onClick. |
@@ -1,4 +1,5 @@ | |||
import React, { PropTypes } from 'react' | |||
import invariant from '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'd actually use this if you're importing it ;)
location.query | ||
) | ||
isActive: (location, to, props, router) => { | ||
if (router) { |
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 wouldnt check for router here. Move it up to wrap the construction of all props for the eventual a tag. I.e., in the case of no router context, this renders a normal a tag.
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.
@timdorr Do you just want to spread the given props of the Link onto an anchor when there is no router and return that? Also, should it warn that it is doing this? If so, how should we go about firing a warning, console.warn
causes the build to fail
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.
@timdorr If we just spread, to won't ever get transformed to href or anything.
Hey @timdorr and @ryanflorence, I have iterated on this and feel as if I have found a solution that I feel will benefit users. Fixes in Link When outside of a
Effect of these changes A Questions
|
event.preventDefault() | ||
this.context.router.transitionTo(this.props.to) | ||
} | ||
} else { |
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.
just drop the else, most likely we're just in a unit test and we don't want warnings anyway.
@@ -79,12 +87,15 @@ class Link extends React.Component { | |||
} = this.props | |||
|
|||
const currentLocation = location || this.context.location | |||
let isActive = false |
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.
const isActive = router ? getIsActive(...) : false
I think avoiding a let
is worth a ternary, but idc that match, do it however you like.
className={isActive ? | ||
[ className, activeClassName ].join(' ').trim() : className | ||
} | ||
className={className + (isActive ? " " + activeClassName : "")} |
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.
This will create classNames like " active"
and people will get mad, so trim it or just put it back.
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.
for instance, when there is no className
-> <Link activeClassName="active"/>
5026c5c
to
d970305
Compare
d970305
to
cc14309
Compare
@ryanflorence I made the suggested changes, but will have to look into why the tests failed. It is working perfectly fine for me locally when I build my changes and test RR using my build. |
@Connorelsea I'm going to close this out, as I think we're close to good with the current version. Once #3986 lands, it basically does the same thing. But it's updated against recent changes. If it doesn't work, we'll need to rebase and refactor this branch anyways. |
Fixes #3889
Previously, using a Link outside of the Router would break the program. The breaking was caused by the inability to find location when outside of the router context. This PR fixes that by checking if the context contains the router, and if not, marking "isActive" as false by default. This introduces the assumption that all outside of the router are not active.
This also adds a warning to indicate that the user is using the is being used outside of the error. I am not exactly sure what copy to use in the error.
As of right now, since there is no invariant in handleClick, a can be "used" outside of a router. It simply reloads the page based on the href.
This is my first time contributing, so let me know if there is anything else that needs to be changed or added to resolve this issue.