3 ms·
> It is most definitely not a one-liner. const LinkWrapper = ({to, ...rest}) => to.startsWith(window.location.origin) ? <Link to={to} {...rest} /> : <a h
by lopatin 8y ago
> It is most definitely not a one-liner.
const LinkWrapper = ({to, ...rest}) =>
to.startsWith(window.location.origin) ? <Link to={to} {...rest} /> : <a href={to} />;
> So do it for me. There’s no reason a routing library should break default behavior.
It's not default behavior for a client side router. Making it automatically scroll to the top would save you a few lines of code while creating a monumental headache for those who need it to retain the scroll position.
- bryanrasmussen 8y agoI guess you might need a polyfill for window.location.origin depending on target browsers, and since they discussed relative urls earlier I guess they want to handle relative urls as well and probably want to also handle starts with //. I think generally what happens, based on what I often end up doing and what I've seen in bigger projects that depend on React Router, is you make a linker component to handle issues, and in fairness you end up not having a 1 liner then.
- johnfn 8y agoThis has a number of bugs. Links don’t have to start with window.location.origin, they can just start with /. You didn’t pass in ...rest into the a tag, but oh did you know that the props of a are not the same type as the props to Link? They’re not - so even if you did that it wouldn’t have worked. Oh, and don’t forget to handle “javascript:” prefixed links - those are valid too. Oh and good luck getting this to type check with Typescript. You might be able to pull off something fancy with subtraction types, but who knows if it’ll continue to work when react router upgrades next year. You might think I’m being overly pedantic. Perhaps I am - however in the course of my site I ran into each and every one of these issues after starting out with a clever one liner much like the one you provided. And every time I fixed one I wondered why react router couldn’t handle this for me. > Making it automatically scroll to the top would save you a few lines of code while creating a monumental headache for those who need it to retain the scroll position. Maybe I’m just not being charitable enough, but it beats me how a boolean flag is a “monumental headache” but all the above work with absolute links is an obvious thing anyone can do.
- deleted 8y ago[deleted]
- tracker1 8y agoI understand the frustration.. that said, if you use a `javascript:` link in a modern web application, you deserve what you get (garbage).