Automatic scrolling for Chat app in 1 line of code + React hook

While using WhatsApp, twitch, or any social media application your chat feed automatically scrolls to the bottom when a new message is sent/received. While building an application with a chat feature this is definitely an important feature you should s…


This content originally appeared on DEV Community and was authored by Deepankar Bhade

While using WhatsApp, twitch, or any social media application your chat feed automatically scrolls to the bottom when a new message is sent/received. While building an application with a chat feature this is definitely an important feature you should ship.

If you don't understand what I am actually talking about try out this little demo I made. Type a message and press enter, as you send a new message it goes out of view and you have to scroll to view it.

If you want to try this interactive demo live head over to my original blog post.

Chat before

It's actually pretty simple to fix this, firstly we should know the container element which is wrapping all the chats. Then select the element, get the height using scrollHeight then set the element's vertical scroll height using scrollTop.

That's it.

const el = document.getElementById('chat-feed');
// id of the chat container ---------- ^^^
if (el) {
  el.scrollTop = el.scrollHeight;
}

Here's the new demo with this thing implemented. Now it scrolls to the bottom when a new message comes in.

Chat After

Now coming to the react implementation, we will use useRef & useEffect to get access to the element and handle the side effect.

This would take dep as an argument which will be the dependency for the useEffect and returns a ref which we will pass to the chat container element.

import React from 'react'

function useChatScroll<T>(dep: T): React.MutableRefObject<HTMLDivElement> {
  const ref = React.useRef<HTMLDivElement>();
  React.useEffect(() => {
    if (ref.current) {
      ref.current.scrollTop = ref.current.scrollHeight;
    }
  }, [dep]);
  return ref;
}

Usage of the above hook:

const Chat = () => {
  const [messages , setMessages] = React.useState([])
  const ref = useChatScroll(messages)
  return(
    <div ref={ref} >
      {/* Chat feed here */}
    </div>
  )
}


This content originally appeared on DEV Community and was authored by Deepankar Bhade


Print Share Comment Cite Upload Translate Updates
APA

Deepankar Bhade | Sciencx (2021-11-25T13:10:18+00:00) Automatic scrolling for Chat app in 1 line of code + React hook. Retrieved from https://www.scien.cx/2021/11/25/automatic-scrolling-for-chat-app-in-1-line-of-code-react-hook/

MLA
" » Automatic scrolling for Chat app in 1 line of code + React hook." Deepankar Bhade | Sciencx - Thursday November 25, 2021, https://www.scien.cx/2021/11/25/automatic-scrolling-for-chat-app-in-1-line-of-code-react-hook/
HARVARD
Deepankar Bhade | Sciencx Thursday November 25, 2021 » Automatic scrolling for Chat app in 1 line of code + React hook., viewed ,<https://www.scien.cx/2021/11/25/automatic-scrolling-for-chat-app-in-1-line-of-code-react-hook/>
VANCOUVER
Deepankar Bhade | Sciencx - » Automatic scrolling for Chat app in 1 line of code + React hook. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/25/automatic-scrolling-for-chat-app-in-1-line-of-code-react-hook/
CHICAGO
" » Automatic scrolling for Chat app in 1 line of code + React hook." Deepankar Bhade | Sciencx - Accessed . https://www.scien.cx/2021/11/25/automatic-scrolling-for-chat-app-in-1-line-of-code-react-hook/
IEEE
" » Automatic scrolling for Chat app in 1 line of code + React hook." Deepankar Bhade | Sciencx [Online]. Available: https://www.scien.cx/2021/11/25/automatic-scrolling-for-chat-app-in-1-line-of-code-react-hook/. [Accessed: ]
rf:citation
» Automatic scrolling for Chat app in 1 line of code + React hook | Deepankar Bhade | Sciencx | https://www.scien.cx/2021/11/25/automatic-scrolling-for-chat-app-in-1-line-of-code-react-hook/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.