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

121
Visualizações
Unable to cleanup subscription in my react native app

In my react native app I have subscribed to CartProduct data model inside the useEffect hook and using a return function I have canceled the subscription which should run when the component unmounts. But this doesn't seem to work because when I change the CartProduct model from a different screen I can see in the console that getCartProduct() run. How to fix this?

const[cartProducts, setcartProducts] = useState <CartProduct[]>([]);
  
const currentUserId = "28ab9cfc-6f0e-4cb4-9ac8-c875aecc7"

const getCartProduct = async () => {
    
    await DataStore.query(CartProduct, c=>c.userID("eq", currentUserId)).then(setcartProducts);
    console.log("fetching!")
    };
  
  
  useEffect(() => {
    
    const subscription = DataStore.observe(CartProduct, d=>d.userID("eq", currentUserId)).subscribe(msg =>
     getCartProduct(),
    );
    return subscription.unsubscribe;
   }, []);

   
 useEffect(() => {

      getCartProduct()
         
    }, []);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Did you try to wrap the useEffect cb with an fat arrow function?

  useEffect(() => {
    
    const subscription = DataStore.observe(CartProduct, d=>d.userID("eq", currentUserId)).subscribe(msg =>
     getCartProduct(),
    );
    return ()=> {
     console.log("unsubscribe -> ")
     subscription.unsubscribe()
    };
   }, []);

   
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