首页 > 解决方案 > 在 ReactJs 组件中滚动到顶部

问题描述

我有很多搜索过滤器和一个分页组件。当用户单击分页组件而不是搜索过滤器组件时,我想滚动到顶部。现在,即使用户点击搜索组件,页面也会跳到顶部。非分页点击时如何防止页面滚动到顶部?我正在使用一个类组件。

...
 constructor(props) {
        super(props)
        this.myRef = React.createRef()  
    }
 scrollToMyRef = () => window.scrollTo(0, this.myRef.current.offsetTop)   
  render() {
        return <div>
        <div id="search"><Search1 .../><Search2 .../></div>
        <div ref={this.myRef}><Pagination /></div> 
     </div>
    }  

标签: reactjs

解决方案


推荐阅读