Simplebar: ScrollTop with React

Created on 2 Nov 2019  路  2Comments  路  Source: Grsmto/simplebar

I'm importing and showing Simplebar as follows

import SimpleBar from 'simplebar-react';
import 'simplebar/dist/simplebar.min.css';

class MessageContainer extends Component{
  ref;

  constructor(props) {
    super(props)
    this.ref = React.createRef()
  }

  renderMessages = () => {
    ....
  }

  render() {
    return (
      <SimpleBar autoHide={false} id={`scrollable`} ref={this.ref}>
        {this.renderMessages()}
      </SimpleBar>
    )
  }
}
````
I'm trying to make it scroll to the bottom when the component updates with the following code:

componentDidUpdate(prevProps, prevState, snapshot) {
cont scrollEl = this.ref.current.el;
scrollEl.scrollTop = scrollEl.scrollHeight;
}
```

But it doesn't want to work. As soon as I saw for a pair of divs, it does work.

Hope you can tell me what im doing wrong.

question

All 2 comments

Oke, so i've got it to work by grabbing the content wrapper like const scrollEl = this.ref.current.el.getElementsByClassName('simplebar-content-wrapper')[0]; and applying the scrollTop there

You are calling scrollTop on the wrong element. It should be the following:
this.ref.current.getScrollElement()

See doc

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Aleksandr2015 picture Aleksandr2015  路  6Comments

FuturFuturFutur picture FuturFuturFutur  路  4Comments

JoshClose picture JoshClose  路  7Comments

Blackleones picture Blackleones  路  3Comments

apopelyshev picture apopelyshev  路  7Comments