如何在 react-simplebar 中获取滚动属性(在有状态函数中)

How to get scroll properties in react-simplebar (in stateful function)

我是 react.js 中 refs 的新手,在 react-simplebar 文档中它只是展示了如何获取无状态函数的滚动引用。

class App extends React.Component {
  render() {
    console.log(this.refs.scroll)    // => Undefined
    return (
      <Simplebar ref={this.refs.scroll}><h1>scrollable element</h1></Simplebar>
    )
  }
}

试试这个

class App extends React.Component {
constructor(props) {
        super(props);
        this.scrollableNodeRef = React.createRef();
    }
  
  onChangeScrollToTop() {
        this.scrollableNodeRef.current.scrollTop = 0;

    }
  
  
  render() {
    console.log(this.refs.scroll)    // => Undefined
    return (
      <Simplebar scrollableNodeProps={{ ref:this.scrollableNodeRef }}>
      <h1>scrollableelement</h1>
      </Simplebar>
    )
  }
}