-
-
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 the to
prop optional
#2319
Comments
For what it's worth, a workaround is to set up a wrapper : export class OptionalLink extends React.Component {
render( ) {
return this.props.to
? <ReactRouter.Link {... this.props} />
: <a { ... this.props} />;
}
}; Not so pretty, though :( |
Those aren't equivalent - |
@taion If you mean "anchor" as a synonym for a
So currently, |
Interesting. Okay. |
quick hack: <Link to=' '>Nothing</Link> |
but to be honest I don't see the point of having that |
It does seem a little pointless. Maybe this can be shelved for whatever pending re-write of |
please give me a use case for using an empty |
Well, any time you may or may not have a link, which is quite often if you write generic components (as we could find in libraries). For example, in my case, I have a grid component. Each cell may or may not be a link, depending on its content. In the current state, I have to either duplicate my code to handle the splitted is-a-link / is-not-a-link logic, or wrap the Link component to add this feature myself (which is what I did, with the same code that the one in the second comment). Frankly, giving the Link source code a cursory read, I feel like that supporting this use case wouldn't add any sort of complexity (just an early return with |
Sure, there's others ways to do it (mostly hacks, as you said in your first post). But I already know how to get this behaviour if I want to. I see this issue more as a discussion over the behaviour that should be expected of a Link without a |
I get your point, let's see what others think about it, I'm happy to add it, just want to see other opinions 😄 This is what we are looking at: http://www.w3.org/TR/html-markup/a.html#placeholder-hyperlink |
My 2 cents -
So thinking about this a bit more, I'd be against adding this to React Router. |
I think the "2 cents" from @taion are the strong points, closing. @mjackson @ryanflorence if you think that's something we should include, please reopen. |
Styling links with hrefs vs links w/o is trivial: a {
// styles for all links, including placeholder links
}
a[href] {
// styles for only links that actually have hrefs
} Indeed browser default (user-agent) stylesheets already make a visual distinction. Assistive Technology can handle a link with no href, and browsers like Chrome and Firefox are not inaccessible out-of-the-box.
I haven't used the React Router enough to comment on the ergonomics of external links, but as a web author I've come across a few scenarios when I knew a thing ultimately would link somewhere, but didn't have a valid URL at the exact moment. I've never come across one where I didn't know for sure whether the link was internal or external. I guess it could happen, I'll try to keep my mind open. Regardless, making a new component to handle external links seems like a smart encapsulation of concerns, as the definition of that problem is things that are external to the router. Handling placeholder links within the router context seems firmly within the realm of things that the library should accommodate. The W3C and WHATWG decided that placeholder links are totally valid. Whether you agree with the usefulness of this feature or not, is it wise for React to deviate from the HTML standard? W3C https://www.w3.org/TR/html-markup/a.html#a.attrs.href The workarounds for it are simple but lame. Please consider again adding this to your library. |
I'm open to reconsidering this. Thoughts @reactjs/routing? |
Apologies in advance if any of my nomenclature is wrong. I second @macgyver's suggestion. The main issue is that I know something is a link but maybe the url is generated after an action fires on the component that is supposed to mount. A javascript error is going to get thrown because that The fact of the matter is that component design is literally built around the idea of being able to pass null values until you receive the data you need from some ajax call exists. Your only argument then is that you conditionally just create a new element or just have it not display until it's ready? That's a bad solution. After the component mounts, the user is going to just see that link pop up out of nowhere, or switch with some other element (which it shouldn't for UX purposes), unless the element is identical. But if you're going out of your way to make an element that is identical, what is the point? If you're designing whatever substitute element you need there to look like a normal link, then it just makes sense the Link element handle that. It should have the same interface as an anchor tag. The fact that most people haven't run into the issue just means most people are either doing the suboptimal solutions I previously ran into, or waiting until all your requests are complete before showing the page, or you're not generating any dynamic urls after the component mounts |
This is an accepted feature request. If you want it, make a PR. |
some context: it wasn't reverted, per se, in #4492, as that was happening on the v4 branch, which was a complete rewrite. that pr was opened to prevent the runtime exception that would occur within the |
I explained above in my response. But let's try again, bullet point style.
From the HTML 4.01 spec (just as true for HTML 5):
Now all that being said, it's not too hard for developers to handle this themselves.
But this seems weird for |
I just wanted to chime in with our use case. We have elements which support loading skeletons, part of the reasons of having the skeletons is to minimise the amount of DOM changes when we finish loading. Our components are wrapped in a |
@JakeGinnivan that's an exact use case for what I was trying to get across in the post I made above yours. I don't think the authors are a fan but it's been half a year and I haven't gotten a response to the points I made. Which is fine, we're all busy. But I wouldn't wait for them to implement this feature if you need this solved now. While not ideal, I'd add a utility component called
Then you return ButtonElement and spread all the props on it. Easy peasy. |
Could it be possible to make the
to
prop optional on the Link component (if missing, nothing would happen, just like a<a/>
tag without itshref
attribute)? It would be useful when writing generic components, that may or may not be links.The text was updated successfully, but these errors were encountered: