Search code examples
reactjsrefsimplebar

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


I am new with refs in react.js and in the react-simplebar documentation it just shows how to get the scroll ref for a stateless function.

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


Solution

  • try this

    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>
        )
      }
    }