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

345
Views
Problem with scrollIntoView not scrolling until the end on React

I have one component that builds a list of messages for a chat, the scrollIntoView function only works when I open the chat for second time, (the first time, it doesn't scroll until the end). I attached some photos and videos and some code.


    const queryClient = useQueryClient()
    const { data, isLoading } = useChatMessages(id)
    const { socket } = useSocket()
    const viewRef = useRef(null)

    useEffect(() => {
        socket.on('receive_message', (data) => {
            queryClient.setQueryData(['CHATMESSAGES', id], function(oldData) {
                return [...oldData, data]
            })
        })
    }, [socket])

    useEffect(() => {
        viewRef.current && viewRef.current.scrollIntoView({behavior: 'smooth', block: 'start'})
    }, [data])

    if (isLoading) return <span>Loading...</span>


    return (
        <div className={styles.chatMessages}>
            {data.map(message => (
                message.sender_id !== 'x' ? <MessageReceived key={message.id} date={message.date} message={message.message} /> : <MessageSended key={message.id} date={message.date} message={message.message} />
            ))}
            <div ref={viewRef} />
        </div >
    )
}

Video: https://i.imgur.com/p3qqGZn.gif (As you can see, I need to scroll manually until the end) Can someone help me resolving the error?

about 4 years ago · Santiago Gelvez
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!