0

I am trying to scroll div to button when page loaded and added a new message in the list.

but here I am getting error 'scrollIntoView' undefined.

This is my code what I have tried so far

     componentDidUpdate() {
       this.scrollToBottom ()
      }

     componentDidMount() {
       this.scrollToBottom ()
      }

       scrollToBottom = () => {
      this.messagesEnd.scrollIntoView({ behavior: "smooth",  });
     } 
  <div style={{ float:"left", clear: "both" }}
  ref={(el) => (this.messagesEnd = el)}>
  </div>

what Is wrong here why it is showing this error.

Your help would be highly appropriated

7
  • 1
    Where is that.messagesEnd defined? Commented Sep 26, 2019 at 9:28
  • 1
    Welcome Kuldeed! Thanks for sharing your problem with us. We'd like to help you. Therefore we will need more information as well as a more specific problem description: 1) Could you copy-paste the exact error message? 2) Could you add more lines of code? 3) Your text is kind of hard to read. May I recapture it and send you a proposal how I have read your question? Commented Sep 26, 2019 at 9:31
  • @Dario Sorry it is `this' I have updated my question Commented Sep 26, 2019 at 9:33
  • @MichaelCzechowski Hi, Thanks for your reply, Actually here I want to scroll to bottom when component loaded and new item added in list, so after researching on internet I have came across with this method but facing issue ScrollView undefined Commented Sep 26, 2019 at 9:44
  • In order to get help you should provide your messagesend as well Commented Sep 26, 2019 at 10:01

2 Answers 2

4

You can try to adjust scrollToBottom() method like:

scrollToBottom = () => 
      this.messagesEnd && this.messagesEnd.scrollIntoView({ behavior: "smooth",  });
Sign up to request clarification or add additional context in comments.

Comments

1

I got solution of my problem. HTML

 <div className="main-comment" ref={(elem) => this.container = elem}>
    //Your another div or content goes here
      </div>

in.ts page code

componentDidMount(){
this. scrollToBottom()
}
 componentDidUpdate(){
    this.scrollToBottom()
    }


 scrollToBottom = () => {
      if(this.container){
      this.container.scrollTop = this.container.scrollHeight
       }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.