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.

Url hash is changing, but viewport not scrolled

See original GitHub issue

Hi. I have 15 React and 3 React Router. Do you supported it?

"react": "^15.4.2",
"react-router": "^3.0.2",

I have this markup:

<ul className="links">
          <li>
            <a href="#link1">
              Link 1
            </a>
          </li>
          <li>
            <a href="#link2">
              Link 2
            </a>
          </li>
          <li>
            <a href="#link3">
              Link 3
            </a>
          </li>
          <li>
            <a href="#link4">
              Link 4
            </a>
          </li>
          <li>
            <a href="#link5">
              Link 5
            </a>
          </li>
        </ul>
<ScrollableAnchor id={"link1"}>
          <div>
.......
          </div>
        </ScrollableAnchor>

So it’s not working. No errors in console. Url hash is changing, but viewport not scrolled

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:5

github_iconTop GitHub Comments

3reactions
JonatanSalascommented, Jun 5, 2017

I have ended up with my own solution:

import React from 'react';
import { withRouter } from 'react-router-dom';

class ScrollToAnchors extends React.Component {
    componentDidUpdate(prevProps) {
        if (this.props.location !== prevProps.location) {
            const element = document.querySelector(this.props.location.hash);

            if (element) {
                element.scrollIntoView({
                    behavior: 'smooth'
                });
            }
        }
    }

    render() {
        return this.props.children;
    }
}

export default withRouter(ScrollToAnchors);

Also, for supporting smooth-scroll in all browsers I have added a polyfill.

UPDATE:

You can also use Jump.js and refactor the previous component to this new one:

import React from 'react';
import jump from 'jump.js';
import { withRouter } from 'react-router-dom';

class ScrollToAnchors extends React.Component {
    componentDidUpdate(prevProps) {
        if (this.props.location !== prevProps.location) {
            const element = document.querySelector(this.props.location.hash);

            if (element) {
                jump(element, {
                    duration: this.props.duration || 500,
                    offset: this.props.offset || 0,
                    callback: this.props.callback || undefined,
                    a11y: this.props.a11y || false
                });
            }
        }
    }

    render() {
        return this.props.children;
    }
}

export default withRouter(ScrollToAnchors);
2reactions
sholzmayercommented, Aug 2, 2017

I had the same problem. Do you have a fixed navigation and the content is overlapped, so it isn’t visible - but shown under the navbar?

This worked for me:

import { configureAnchors } from 'react-scrollable-anchor'; configureAnchors({ offset: -64 });

Read more comments on GitHub >

github_iconTop Results From Across the Web

Modifying location.hash without page scrolling - Stack Overflow
Use history.replaceState or history.pushState * to change the hash. This will not trigger the jump to the associated element.
Read more >
Hash Tag Links That Don't Headbutt The Browser Window
The browser window will scroll itself (instantly) into such a position where the element with the ID of “section-two” is visible. It scrolls...
Read more >
How to Enable Smooth Scrolling to Hash Link for One-Page ...
Enable smooth scrolling to hash links from Manage ⪧ Settings ⪧ Global Styles ⪧ Scripts.Join our Facebook group for community support: ...
Read more >
Flickity · Options
It already is included with flickity.pkgd.js , but not with Flickity as a stand-alone package. ... View CodePen in debug mode to see...
Read more >
React Scroll Full Page
Structural changes of cells undergoing necrosis and apoptosis. js-scroll ... React Router Dom: Scroll To Top on Route Change. com/fullPage/ ), but it...
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