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.

Scroll to bottom (showing newest message) when calling prepend or concat earlier messages

See original GitHub issue

Issue Description

When im updating list with prepend or concate method, the view is scrolling to bottom.

Steps to Reproduce / Code Snippets

Chat View

<GiftedChat
              messages={this.state.listMessage}
              onSend={messages => this.onSend(messages)}
              renderFooter={this.renderFooter}
              scrollToBottom
              inverted={true}
              user={{
                _id: authUser._id,
                name: authUser.name
              }}
              listViewProps={{
                onEndReached: debounce(canLoadMore ? () => this.loadMoreContent(chatroomActive) : () => {}, 250),
                onEndReachedThreshold: 0.1,
            }}

loadMoreContent function

loadMoreContent = async (item) => {
    const { page } = this.state;
    const query = {
      populate: 'file sender.profile.photo',
      q: `channel=${item._id}`,
      limit: 20,
      page,
    };
    const { data } = await fetchApi.get('/collaboration/chat', query);
    if (data && data.status === 'success') {
      const messages = await data.data.collaborationlog.map(x => ({
        _id: x._id,
        text: x.message,
        createdAt: x.createdAt,
        user: {
          _id: x.sender._id,
          avatar: item.avatar
        },
        system:x.type=='call',
      }));
      if (page > 1) {
        this.setState(prevState => ({
          listMessage: prevState.listMessage.concat(messages),
        }));
      } else {
        this.setState({ listMessage: messages });
      }
      this.setState({
        page: page + 1,
      });
      const { totalPage } = data.data;
      if (totalPage > page) {
        this.setState({ canLoadMore: true });
      } else {
        this.setState({ canLoadMore: false });
      }
    } else {
      this.setState({ loading: false });
      Alert.alert(
        'Error',
        'Error fetching data',
        [
          { text: 'Ok' },
        ],
      );
    }
  }

if i replacing this line

this.setState(prevState => ({
          listMessage: prevState.listMessage.concat(messages),
        }));

with

this.setState(prevState => ({
          listMessage: GiftedChat.prepend(prevState.listMessage, messages)
        }));

i still get same result.

Expected Results

When scroll reach top of list, its fetching data update listMessage and stay in current scroll position

Additional Information

  • Nodejs version: v10.16.3
  • React version: 16.9
  • React Native version: 0.61.1
  • react-native-gifted-chat version: 0.9.11
  • Platform(s) (iOS, Android, or both?): Android
  • TypeScript version: -

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:7
  • Comments:12

github_iconTop GitHub Comments

1reaction
deberoppa7commented, Dec 10, 2019

@cicomint , I was struggling with the same issue. the fix is to do this : listViewProps={{ ref : null }} And I don’t have any explanation why it works !

1reaction
raienkocommented, Oct 28, 2019

same issue - ios/android simulators

Read more comments on GitHub >

github_iconTop Results From Across the Web

scroll down bottom when new message append - Stack Overflow
You can sum height of each <li> element and then use it. Here is an example: var ht = 0; $(".messages li").each(function() {...
Read more >
Pin Scrolling to Bottom - CSS-Tricks
We can force a scrolling element to stay pinned down to the bottom even as we append new content. Chat is a classic...
Read more >
Scroll Events and Intersection Observer - Beginner JavaScript
In this video we will learn about scroll events. A scroll event is when someone goes ahead and scrolls on the page or...
Read more >
Combine text values by using an expression - Microsoft Support
When you want to combine the values in two or more text fields in Access, you create an expression that uses the ampersand...
Read more >
Add a standard footer to users' outbound messages
Use the Append footer setting to automatically add footer text to outgoing ... Sign in using your administrator account (does not end in...
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