question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Scroll to hash anchor not working on initial render

See original GitHub issue

The code in client.js supports jumping to an anchor while navigating through the site. However, this code is not hit when initially loading the site with a hash.

I created a fork of react-starter-kit at https://github.com/jorrit/react-starter-kit/tree/hashinit where the footer has an id attribute. The header contains a Link component to #footer and clicking this link works fine.

However, visiting http://localhost:3000/#footer on a new tab doesn’t scroll to the footer.

Should React take care of scrolling to the anchor on first load? Or should the code in client.js that scrolls to an anchor also be executed when isInitialRender = true?

Finally, I added a regular <a href="#footer"> to the navigation bar. This one doesn’t work at all, because regular hash changes cause a POP history event and <Link to="#footer"> causes a PUSH event. The condition if (action === 'PUSH') { should be extended to check for location changes that indicate a hash change.

I could make some PR’s if you like but perhaps the direction of the solution should be discussed first because there are different ways to handle this.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
frenzzycommented, Jan 14, 2018

I believe the browser should care about scrolling to hash at initial page load. If it does not work, then probably disabling scroll restoration code must be executed right after initial page render but not before as it is now.

0reactions
jorritcommented, Jan 24, 2018

Thanks, I was also thinking about the situation where <a> tags are present in data coming from a CMS or something like that. I think onLocationChange can be amended to take into account these kinds of location changes. I don’t yet need this myself and I have the feeling that it isn’t a priority for you either, so I will not create a PR for it now. Maybe later.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Scroll position on initial page load does not go to hash/anchor ...
javascript - Scroll position on initial page load does not go to hash/anchor position when directly accessed the URL in nuxt app -...
Read more >
How to load a #hash fragment to an anchor name in react ...
This behavior occurs for several reasons, one reason is because the anchor name offset is executed after the page loads the first DOM,...
Read more >
Scrolling to an Anchor in React When Your Elements Are ...
This hook uses React's useEffect. It reads the current location from react-router with useLocation . Because useEffect has no dependencies, it runs on...
Read more >
Guide to scroll anchoring - CSS: Cascading Style Sheets | MDN
If your page is not behaving well with scroll anchoring enabled, it is probably because some scroll event listener is not handling well...
Read more >
react-scroll - npm
Start using react-scroll in your project by running `npm i react-scroll`. ... React component for animating vertical scrolling ... With-hash.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found