Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

63
Visualizações
Implement React's UseEffect to listen only to an increment / decrement

I've built a chat application and I'm using a useEffect to listen to messages.length, in order to scroll down to bottom upon an arrival of a new message - an increment in messages.length.
However, there's also the case where I delete a message, a decrement in messages.length. Meaning, I get scrolled down to bottom upon deletion too, and I would like to avoid that kind of behavior. The messages are stored within my redux store and accessible as a prop.

Is there a way for the useEffect to listen only on an increment?
Is there a way to have access to the messages' previous state?
Or is storing a prevLength variable unavoidable?

Here is the part of my code that needs revising:

// Scroll to bottom upon new message:
useEffect(() => {
    scrollDown();
}, [messages.length]);

This is the entire code:

import { useState, useEffect} from 'react';
//Components:
import ChatBubble from './ChatBubble';
import ScrollButton from './ScrollButton';

export default function MessagesBoard({ messages }) {

  //useState:
  const [showScrollButton, setShowScrollButton] = useState(false);

  //Event: Scroll to bottom upon new message
  useEffect(() => {
    scrollDown();
  }, [messages.length]);

  const scrollDown = () => {
    ....
  };

  return (
    <div className='chatMessagesBlock'>
      <div className='messagesCanvas'>
        {messages.map((chatMessage) => {
          return (
            <ChatBubble
              key={msgID}
              msgID={msgID}
              sender={sender}
              msgContent={msgContent}
            />
          );
        })}
      </div>

      <ScrollButton
        showScrollButton={showScrollButton}
        scrollDown={scrollDown}
      />
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You could keep track of the previous length in a useRef:

const prevLength = useRef(0);
useEffect(() => {
    if (prevLength.current < message.length) {
        scrollDown();
    }
    prevLength.current = messages.length;
}, [messages.length]);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda