change scrollTop in reactjs

前端 未结 4 1410
隐瞒了意图╮
隐瞒了意图╮ 2021-01-04 03:36

I just learn react, and want to achieve a function : both A,B are components, if A scroll, then B scroll

The following is my code

<

4条回答
  •  清歌不尽
    2021-01-04 04:09

    this.refs is deprecated. use reactjs.org/docs/refs-and-the-dom.html#creating-refs

    import React from 'react';
    
    class SomeComponent extends React.Component {
      constructor(props) {
        super(props);
        this.resultsDiv = React.createRef();
      }
    
      someFunction(){
         this.resultsDiv.current.scrollIntoView({behavior: 'smooth'});
    
         // alternative:
         // this.resultsDiv.current.scrollTop = 0;
      }
    
      render() {
        return (
          
    ); } } export default SomeComponent;

提交回复
热议问题