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

146
Visualizações
how to prevent stacking function callbacks while exiting a screen react/react-native

I have a local state selectedProducts holding a number representing the number of products the client has selected, while exiting the screen I want to update the number in redux so it can be viewed in the cart on a different screen.

but every time my local selectedProducts updates it stacks another function call on beforeRemove event

I know this is a noob problem but I've spent hours trying to find a solution to this problem and advice would be very helpful :)

    useEffect(()=>{
     navigation.addListener('beforeRemove', () => {
     console.log('check this', selectedProducts);
     if (!selectedProducts) {
      return;
     }
     dispatch(addToCartMultipleQty(selectedProducts));
     });
    },[selectedProducts])

selectedProducts is a number state whose initial value is null and on a button click event its value is either incremented or decremented by 1, if its previous value is null then its value is set to 1

Note: I just want to update selectedProducts state's latest value only once in redux when the screen is about to be exited/unmonted

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can try this:

useEffect(()=>{
  navigation.addListener('beforeRemove', () => {
    console.log('check this', selectedProducts);
    if (!selectedProducts) {
      return;
    }
    dispatch(addToCartMultipleQty(selectedProducts));
  });  
  return () => {
    navigation.removeListener('beforeRemove');
  }
}, [selectedProducts])
about 4 years ago · Juan Pablo Isaza Relatório

0

Add that in return at the end of useEffect it will work as componentWillUnmount in functional component

useEffect(() => {
    return () => {
        // Anything in here is fired on component unmount.
    }
}, [])

Edit: In Your case

  useEffect(() => {
    console.log('check this', selectedProducts);
    if (!selectedProducts) {
      return;
    }
    return () => {
      // Anything in here is fired on component unmount.
      if (selectedProducts) {
        dispatch(addToCartMultipleQty(selectedProducts));
      }
    };
  }, [selectedProducts]);
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