-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
Make to prop on Link optional again #5271
Conversation
to
prop optional again@@ -58,6 +58,10 @@ class Link extends React.Component { | |||
render() { | |||
const { replace, to, ...props } = this.props // eslint-disable-line no-unused-vars | |||
|
|||
if (to == null) { | |||
return <a {...props} href={null} /> |
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.
Any reason to have the href={null}
prop set? Seems unnecessary.
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.
Set to null to avoid mistype when you pass href
prop instead of to
. Link with non-empty to
always generate it's own href
, so null is required only for empty one.
Thanks for the tests! Just need that question above answered and then LGTM. |
I'm not a fan of this. #5240 brought something about this up as well, and I just don't see the point. A Edit Maybe that comes off as overly negative. I am open to being convinced that this would be beneficial, but so far I haven't come up with a compelling reason to support this. |
@pshrmn While it looks like a "hack" of component architecture it's pretty pragmatic for something like a list with disabled elements. Otherwise i have to write a wrapper for Link and i think that empty import { Link } from 'react-router-dom';
const ListItem = (props) => {
var { id, availableQuantity, ...rest } = props;
var outOfStock = !availableQuantity;
return (
<Link className="list-item" to={outOfStock ? null : `/item/${id}`>
{...}
</Link>
);
}; Also i can call preventDefault inside onClick but it's confusing to have an non-clickable item with link in browser status bar. |
What kind of issues can occur if we implement this? |
@sanniassin if link is not meant to be matched and active why do you need it? Usually you don't need to render anchor at all if , for example, your products are out of stock. {outOfStock ? (<span className="link">Out of stock</span>) : (<Link to="/item">Item</Link>)} |
@zemd It doesn't scale well for more complex components. <Link to={outOfStock ? null : `/item/${item.id}`} className="item">
<img className="item__picture" src={item.imageUrl} />
<div className="item__name">{ item.name }</div>
<div className="item__description">{ item.description }</div>
<div className="item__tags">
{ item.tags.map((tag, i) =>
<div className="item__tags-item" key={i}>{ tag }</div>
)}
</div>
</Link> vs outOfStock ? (
<div className="item">
<img className="item__picture" src={item.imageUrl} />
<div className="item__name">{ item.name }</div>
<div className="item__description">{ item.description }</div>
<div className="item__tags">
{ item.tags.map((tag, i) =>
<div className="item__tags-item" key={i}>{ tag }</div>
)}
</div>
</div>
) : (
<Link to={`/item/${item.id}`} className="item">
<img className="item__picture" src={item.imageUrl} />
<div className="item__name">{ item.name }</div>
<div className="item__description">{ item.description }</div>
<div className="item__tags">
{ item.tags.map((tag, i) =>
<div className="item__tags-item" key={i}>{ tag }</div>
)}
</div>
</Link>
) |
@sanniassin it scales indeed as it should scale. if you have some common pieces of code you can always utilize them. |
@zemd I guess it depends on a preferred code style. Link is a replacement for an anchor tag and anchor can have no href, so it sounds intuitive for me to expect the same behaviour from Link. Empty |
An argue to implement in related issue: #2319 (comment) Any decisions? |
@sanniassin is this not working for you? outOfStock ? (
<ProductItem item={item} />
) : (
<Link to={`/item/${item.id}`} className="item">
<ProductItem item={item} />
</Link>
) |
@vladshcherbin It works, but it means that it's a |
Thanks for the PR @sanniassin, but I think I'd suggest you do as @vladshcherbin (and others) suggest. If you don't need |
@mjackson Should we add a note to migration guide then? Because v3 supports empty |
@sanniassin Yes. I wasn't even aware we had a migration guide! (That's awesome that we do though!) |
@zemd I disagree with your assessment. Take a look my comment here for further elaboration: #2319 (comment) To sum it up (again), Now technically sure, the user can wrap Link on their own. This is what I do in my repo if my memory is correct:
That being said, the thing to remember, is Now that being said, I'm not really petitioning for the feature anymore... I'm using a different routing libraries. I just wanted to explain my reasoning, as I made an argument that @sanniassin actually posted in this thread and neither him nor I ever received any response to it, so I was sort of confused coming back here... we continued the conversation it seems like, without a response to the points I made 😄 |
@reywright No, |
@timdorr how so? You can very easily have an click handler for the onClick of an anchor, and inside the handler, e.preventDefault, then use history.push(). In fact if you're working within redux, it makes more sense not to use < It is quite convenient though. |
A reimplementation of #3803 for v4
Fixes #2319