Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!