Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

139
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda