scrollToBottom intermittently not working

I have been developing a chat application and when I open a chat page to show a current conversation I want the page to automatically scroll to the bottom so the user can see the last chat message entered.

export class ChatPage {
  @ViewChild(Content) content: Content;

  constructor() {
  }

  ionViewDidEnter() {
    this.content.scrollToBottom();
  }
}

Unfortunately the scroll occasionally does not execute (logging shows that the ionViewDidEnter event is always executing but the scroll is not). It seems to work most of the time but occasionally when you open the page it scrolls but not all the way to the bottom). This happens on the device as well as the browser. Anyone know what might be going on?

I’ve narrowed the problem down a little. The scroll only fails on conversations that contain images. Even though the images are loaded and displayed ok, sometimes (when the conversation contains images) the scroll fails.

adding a delay before executing the scroll seems to have fixed it (not tested on the device yet)

1 Like

It sounds as though you’re scrolling before the page is rendered. Can you use IonViewDidLoad instead?

I tried that but it didn’t work - adding a 300ms delay before the scroll solved the problem though, thanks.

2 Likes

I’ve been facing the same issue when I choose a segment on a page. It scrolls down only around 10% from the top.

segmentChanged()
    {
    if(this.segment === 'abc'){
         
        this.content.scrollToBottom();
       
    
    }
    }

Adding a delay makes it work fine

 setTimeout(()=>{this.content.scrollToBottom();},200); 
1 Like

See https://github.com/fisshy/react-scroll/issues/373#issuecomment-1031164677