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

207
Visualizações
snackbar, new timer every time it is displayed in React

I have a button to add an item to a wishlist, so when the user clicks on that button, it triggers a process of showing and hiding a snackbar, telling the user the item has been saved successfully. I have a component called AlertWishlist, and from the parent I set a prop called closeWishlistNotification, that basically executes a timeout function to wait 5 seconds and then close the snackbar.

The snackbar itself is based on a state variable called isOpen, a boolean that defines if show or hide the snackbar (if it is true, display, if it is false, hide).

However, the button can be clicked again to remove the item from the wishlist, and this action triggers basically the same process I mentioned before, the only difference is the message the snackbar shows, now saying "Item has been eliminated from wishlist".With all of this, I ended up having this behavior:

  • User clicks button, item is now saved to the wishlist.
  • isOpen = true, display snackbar to notify item added, and start timeout (5 seconds).
  • AT THIS MOMENT, TIMEOUT IS ALREADY 2 SECONDS: User clicks button again, item is now removed from the wishlist.
  • isOpen still true, display snackbar to notify item removed, and start timeout (5 seconds);
  • AT THIS MOMENT, FIRST TIMEOUT HAS BEEN COMPLETED, change isOpen variable to false (because we need to close snackbar)
  • isOpen = false, AT THIS MOMENT, SECOND TIMEOUT IS ALREADY 2 SECONDS, but since isOpen is false, HIDE SNACKBAR.

What I really want is the snackbar to wait 5 seconds always, and if it changes, for example, if the snackbar "Item has been added" is showed and before 5 seconds the snackbar "Item has been removed" replace the first one, then start the time count again.

I am not shure if this question has been answered before since I don't know how to ask properly if I reduce this to pure react, So I decided to use the problem I am having as an example and provide the most possible details.

Any help is appreciated, thanks.

enter image description here

about 4 years ago · Juan Pablo Isaza
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