question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to load messages on scroll?

See original GitHub issue

I tried out the “Load earlier messages” button and it works great, but I am wondering how you would check for when the user scrolls to the top so it can be ran without pressing the button.

Also, is there a way to get the number of messages that GiftedChat currently has without relying on my own state changes e.g. GiftedChat.messages.length? Would also be useful for pagination.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

85reactions
drpioucommented, Sep 29, 2017

Hi, this plugin should implement a loadEarlierMessagesOnScroll, that seems to me a classical function in any chat for loading messages when scrolling top… (can you imagine whatsapp, fb messenger or any other chat only with a click-to-load-earlier-messages button ?)

However, if someone wonders how I achieved it:

isCloseToTop({ layoutMeasurement, contentOffset, contentSize })
{
    const paddingToTop = 80;
    return contentSize.height - layoutMeasurement.height - paddingToTop <= contentOffset.y;
}

render()
{
    return (
        <GiftedChat
            listViewProps={{
                scrollEventThrottle: 400,
                onScroll: ({ nativeEvent }) => { if (this.isCloseToTop(nativeEvent)) this.loadMoreMessage(); }
            }}
        />
    );
}
5reactions
AleksandarSavic95commented, Jun 28, 2019

@drpiou’s solution didn’t work for me, so I made some modifications, and here they are: How to check if scroll is close to top:

const LOAD_EARLIER_ON_SCROLL_HEGHT_OFFSET = 100;
// ...
isCloseToTop({ layoutMeasurement, contentOffset, contentSize }) {
  const contentTopOffset = contentSize.height - layoutMeasurement.height - contentOffset.y;
  // if the screen is not full of messages, offset would be too big
  return contentSize.height < layoutMeasurement.height
    ? contentOffset.y > LOAD_EARLIER_ON_SCROLL_HEGHT_OFFSET // so we only check bottom offset
    : contentTopOffset + LOAD_EARLIER_ON_SCROLL_HEGHT_OFFSET < 0;
}
  • contentTopOffset = how far is the oldest chat message from the top edge: positive number means above, negative number means below.
  • contentOffset.y = the number showing how much content is below the bottom edge of the visible chat area.
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to load messages on mouse scroll? - Stack Overflow
Here is a quick sample code to test $(window).on("scroll", function() { var scrollHeight = $(document).height(); var scrollPosition ...
Read more >
How to Find (and Scroll to) Old Messages on Your iPhone
How to See Old Messages on an iPhone With the Hidden Scroll · Open the Messages app in iOS and select the conversation...
Read more >
How to Find Old Messages on iPhone Without Scrolling
As you tap, the message thread should be quickly scrolled to the very first dialogue of the conversation. For lengthy conversations, a progress ......
Read more >
Infinite Chat Scrollbar. Load messages dynamically and…
//This function helps you scroll to the correct position once a new batch of messages are loaded. var page = 1
Read more >
Load content on page scroll with jQuery and AJAX - Makitweb -
It will automatically load up content whenever the user reaches the bottom of the web page while the page scroll. You can also...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found