简体   繁体   English

reactjs 如何停止自动页面滚动?

[英]How to stop automatic page scrolling in reactjs?

When a page is navigated after a button click, it opens lower, which makes the user to scroll up every time.单击按钮后导航页面时,它会打开较低的位置,这使用户每次都向上滚动。 This happens only in the mobile version and works fine in the desktop version.这仅在移动版本中发生,并且在桌面版本中运行良好。

Since I am new to React and UI, someone suggested to use tabindex = -1 in the link tag, but that didn't work.由于我是 React 和 UI 的新手,有人建议在链接标签中使用 tabindex = -1,但这不起作用。

<div className="Header__logo--mobile hidden-on-desktop">
      <Link to="/">
        <img src={HomeLogoImage} ref={div => (this.imageMobile = div)} />
        <img
          className="Header__logo--glitch"
          src={HomeLogoImageGlitch}
          ref={div => (this.imageGlitchMobile = div)}
        />
      </Link>
</div>

The expectation is to not scroll down when the page opens.期望在页面打开时不向下滚动。 How do I achieve that?我该如何做到这一点?

在要在导航路线上呈现的组件中使用compnentDidMount挂钩。

componentDidMount(){ window.scrollTo(0,0) }

For those who are using the functional components:-对于那些使用功能组件的人:-

useEffect(() => {
  window.scrollTo(0, 0)
}, [])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM