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

120
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
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