-
-
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
Hash links #394
Hash links #394
Comments
Hmm, good question. We should definitely support this for people who are using @rpflorence thoughts? At first glance, it doesn't seem like using |
Yeah, I've always wanted this. Its part of the reason our I don't want to try to support this in But for |
@rpflorence Is anyone working on this? I would be down to form a PR if not. |
Seems like this should already work if you've got |
Yes, it works when you write in the Works:
Doesn't work:
Error:
|
Even with a starting slash it doesn't work for me. Reopen please. |
Yep, doesn't work for me either. Both ways. Shows up in the address bar as it should but never goes to the location on the page. |
Does not work for me either. |
Yeh, this should be reopened. I'm using params to generate a more complex URL and need to add the hash. |
Does not work for me either. This should be reopened please |
+1, I'm also interested in a solution. |
As a workaround, in my main "app", I've added the following to componentDidMount() {
// Decode entities in the URL
// Sometimes a URL like #/foo#bar will be encoded as #/foo%23bar
window.location.hash = window.decodeURIComponent(window.location.hash);
const scrollToAnchor = () => {
const hashParts = window.location.hash.split('#');
if (hashParts.length > 2) {
const hash = hashParts.slice(-1)[0];
document.querySelector(`#${hash}`).scrollIntoView();
}
};
scrollToAnchor();
window.onhashchange = scrollToAnchor;
} This allows you to link to things with URLs like: /#foo#bar which will scroll to elements which have an |
+1 |
Not working with Router.HistoryLocation. It just follows the link and does nothing. |
@nickpresta solution did not work for me, but I could not figure out why. When you are switching from states it will not scroll, but if you refresh the page it will scroll to the element. But I fix it by overriding the scrollBehavior with your solution. var router = Router.create({
routes: routes,
location: Router.HashLocation,
scrollBehavior: {
/**
* Sets the behaviour for scrolling when changing state.
* If there is a second hash in the url, scroll to the element with matching identifier.
* Otherwise scroll to the top.
* @override
*/
updateScrollPosition: function updateScrollPosition() {
window.location.hash = window.decodeURIComponent(window.location.hash);
const hashParts = window.location.hash.split('#');
if (hashParts.length > 2) {
const hash = hashParts.slice(-1)[0];
const element = document.querySelector(`#${hash}`);
if (element) {
element.scrollIntoView();
}
} else {
window.scrollTo(0, 0);
}
}
}
}); |
thanks @Pouja, your solution helped me to solve the issue I was facing. But I'm using HistoryLocation and for me I had to do the following: var router = Router.create({
routes: routes,
location: Router.HistoryLocation,
scrollBehavior: {
updateScrollPosition: function updateScrollPosition() {
var hash = window.location.hash;
if (hash) {
var element = document.querySelector(hash);
if (element) {
element.scrollIntoView();
}
} else {
window.scrollTo(0, 0);
}
}
}
}); |
does this work in 1.0.0? i have something like this:
and updateScrollPosition isn't being called |
@davis in 1.0 there is nothing like |
got it, thanks! |
@scabbiaza not sure if you were able to fix your issue, but if you want to use the route name you can invoke the
|
@alansouzati, your solution worked great. Thank you. |
1.0.0-rc3 actually has a |
@taion how do you use it? |
You pass the hash to it. |
nvm, it's still not working in 1.0.0-rc3 (#2216) |
That's great to hear that react-router 1.0.0 supports that. But I believe some folks will need some time to migrate to react 0.14. In the meantime this work around will help people to get going. |
how to use |
@sanakr Just use a normal |
@timdorr i got solution, its possible in Link .
|
You shouldn't do that. Just use a normal <a href=`mailto:${value}` className={style.qLink} target="_blank">{value}</a> Our |
About hash links, I've had success with using a route param and the ref's callback. (so not really a hash link, but the same functionality)
|
Is there any supported way to prevent re-rendering while using named anchors to sections on the same page? This causes an unnecessary overhead. |
@Agalla81, have you tried using |
@Agalla81 I'm successfully using the following workaround: override Note: If your component is pure this should come out of the box. |
I was wondering if there is now an accepted way to getting anchor links working with react-router? I have attempted many of the methods outlined and failed so had to revert to regular named anchors using id's. I couldn't find much on the internet about this. Would love to know more. Thanks. |
I have tried @RAFREX solution and it appear it work but the view get rerendered while it should not, is there a way to prevent this ? |
A simple solution with https://www.npmjs.com/package/scroll-to-element that a friend found.
After that you add some div with an corresponding |
I did not like the @RAFREX proposal because it requires to use custom https://medium.com/@gajus/making-the-anchor-links-work-in-spa-applications-618ba2c6954a |
Just to add to @abumalick's solution, the |
Yes, my solution is for react-router 3. With react-router 4 you should take history from props |
@RAFREX's solution (#394 (comment)) worked really well, but I didn't want to make such a top level change on a production website just in case there are regressions. I only had one place where I needed this logic, so I used a ref instead that uses similar logic -
|
Although it was not ideal, this worked out of the box for me with both HashRouter and BrowserRouter: https://github.com/rafrex/react-router-hash-link
|
Hey,
I'd like to write something like this:
It should open
home
route and scroll to<section id="about"></section>
Will it be possible ?
Regards, Cezary Daniel Nowak
The text was updated successfully, but these errors were encountered: