Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
Why scrollIntoView works when component first loads but it doesn't onClick?

I'm a creating a chat app and as this kind of apps work every time you send a new message the view should scroll down and show the new message.

The question is that when component renders first time it scrolls down but when I click to send a new message it doesn't. New message is added but it doesn't scroll. What I don't really understand it's that in Firefox it works as expected but it doesn't in Chrome.

Messenger.jsx

const [messages, setMessages] = useState([]);
const scrollRef = useRef();


....
const handleSubmit = async () => {
    const data = {
      conversationId: currentChat._id,
      sender: user._id,
      text: input,
    };

    const response = await newMessage(data);
    setMessages([...messages, response]);
    setInput('');
    setIsDisabled(true);
  };

 useEffect(() => {
    scrollRef.current?.scrollIntoView({ behavior: 'smooth' });
  }, [messages]);


...

<div className='chat-box__top'>
     {messages.length > 0 &&
        messages.map((message) => (
         <div ref={scrollRef} key={message._id}>
            <Message className='message' own={message.sender._id=== user._id} message={message} />
             </div>
      ))}
</div>


....

<button className='chat-box__submit' onClick={handleSubmit} disabled={isDisabled}>
      <SendIcon className='icon' />
---.</button>

CSS of Messenger

.chat-box__top{
   padding-right: 10px;
   overflow-y: auto;
}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!