FaridSafi / react-native-gifted-chat

💬 The most complete chat UI for React Native
https://gifted.chat
MIT License
13.54k stars 3.56k forks source link

In v2.1.0, Changes in message list not automatically triggering the message list to scroll down to bottom/End and no way to add reference to the GiftedChat functional component #2393

Open fahad86 opened 1 year ago

fahad86 commented 1 year ago

Issue Description

Changes in message list is not automatically triggering the message list to scroll down. (Basically scrollToBottom inside GiftedChat.js is not getting triggered on new messages)

Steps to Reproduce / Code Snippets

Expected Results

The list should scroll down to the bottom to reveal the newly sent / received messages

Additional Information

fukemy commented 1 year ago

you can do like this:


useState(() => {
   if(message[0].state === 'sending' && message[0].user._id === currentUserId){
       gitedRef.current.scrollToBottom()
   }
} ,[message]}
fahad86 commented 1 year ago

you can do like this:

useState(() => {
   if(message[0].state === 'sending' && message[0].user._id === currentUserId){
       gitedRef.current.scrollToBottom()
   }
} ,[message]}

We used to do that in the past but with the change to functional components in version 2.1.0 not able to set a reference to giftedChat. Setting a ref yeilds the following error:

ERROR Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?

fahad86 commented 1 year ago

can you please let me know how a reference can be set in the latest version of react-native-gifted-chat

fukemy commented 1 year ago

Im using 0.16.3 and mofidy a lot of code for my job. If you using giftedChat lasted version, you can see the props: scrollToBottom (Bool) - Enables the scroll to bottom Component (Default is false)

fukemy commented 1 year ago

like this

<GiftedChat
 ...
scrollToBottom={(message[0].state === 'sending' && message[0].user._id === currentUserId}
...
/>
karthikkoppa commented 1 year ago

giftedChatRef.current._listRef._scrollRef.scrollTo(1)' & Working with "react-native-gifted-chat": "^2.4.0", & "react-native": "0.71.8", < GiftedChat messageContainerRef={giftedChatRef} />

ahmadardal commented 1 year ago
       const giftedChatRef = useRef<any>();

        <GiftedChat
        ... other code ...
        messageContainerRef={giftedChatRef}
        ... other code ...
       </GiftedChat>

Then anywhere in your code, for example onSend, you can call this

      if (giftedChatRef.current) {
      giftedChatRef.current._listRef._scrollRef.scrollToEnd();
      }